【发布时间】:2017-04-07 04:52:36
【问题描述】:
我有这个对数字进行倒计时的 JavaScript 代码,我希望计数器在数字小于 10 时显示两位数(09、08、...)
(function test() {
setTimeout(function() {
$('#id').text(Number($('#id').text()) - 1);
test();
}, 1000);
})();
//more flexible and modular version
function myTimer(elem, maxtime, indexTime ) {
var i = 0;
test();
function test() {
setTimeout(function () {
elem.text(i);
i++;
if (i < maxtime) {
test();
} else {
console.log('end');
return false;
}
}, indexTime);
}
}
【问题讨论】:
-
检查一个数字是否小于 10 非常简单。将 0 添加到单个数字字符串也非常简单。请解释一下具体哪个部分对您来说比较困难。
标签: javascript html