延时定时器第一秒会出现空档,封装需要执行的代码,先调用一次即可解决
getTime()
setInterval(function() {
getTime()
}, 1000)
function getTime() {
var date = new Date()
var hours = date.getHours()
hours = fn(hours)
var min = date.getMinutes()
min = fn(min)
var sec = date.getSeconds()
sec = fn(sec)
var time = '' + hours + min + sec;
for (var i = 0; i < $('.get').length; i++) {
$('.get').eq(i).attr('src', './img/' + time.charAt(i) + '.JPG')
}
}
function fn(timer) {
return timer < 10 ? '0' + timer : timer
}