1

我希望用户每次在输入运行重置倒计时后键入,我添加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));
})​
4

1 回答 1

2

您在设置超时后立即清除它。尝试这个:

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));

})
于 2012-09-04T22:17:40.087 回答