1. 程式人生 > >解決setTimeout 計時器重複呼叫的問題

解決setTimeout 計時器重複呼叫的問題

不多說直接上程式碼:

var stime = 16,
    t;

function fntime() {
    stime--;
    t = setTimeout(fntime, 1000);
    if (stime < 0) {
        clearTimeout(t);
        return;
    }

    console.log(stime);
}

var timer = function() {
    try {
        if (t._idleTimeout < 0) {
            fntime();
            console.log(1
); } console.log(2); } catch (e) { fntime(); console.log(3); } } timer(); timer(); console.log(t); console.log(t._idleTimeout); clearTimeout(t); console.log(t); console.log(t._idleTimeout); timer(); timer();