我想在用户每次在输入运行重置倒计时中键入后,我添加 clearTimeout
但它不起作用。我该怎么办? (请帮助我编写代码,我不需要新代码。)
演示: http://jsfiddle.net/qySNq/
HTML:
<input id="MizMah">
<div id="Seconds" style="font-size: 80px;">5</div>
js代码:
$('#MizMah').live('keyup', function () {
function render(n) {
var digits = [],
r;
do {
r = n % 10;
n = (n - r) / 10;
digits.unshift([r].join(''));
} while (n > 0);
$('#Seconds').html(digits.join(''));
}
(function timer(current) {
render(current);
if (current > 0) {
myTimeout = setTimeout(function () {
timer(current - 1);
}, 1100);
clearTimeout(myTimeout);
}
}(5));
})
最佳答案
您在设置超时后立即清除它。试试这个:
var myTimeout = null;
$('#MizMah').live('keyup', function () {
function render(n) {
var digits = [],
r;
do {
r = n % 10;
n = (n - r) / 10;
digits.unshift([r].join(''));
} while (n > 0);
$('#Seconds').html(digits.join(''));
}
(function timer(current) {
render(current);
if (current > 0) {
clearTimeout(myTimeout);
myTimeout = setTimeout(function () {
timer(current - 1);
}, 1100);
}
}(5));
})
关于javascript - 在javascript中重置倒计时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12272058/