如果我有一个通过设置的活动超时运行var t = setTimeout("dosomething()", 5000),
无论如何要暂停和恢复它?
有没有办法获得当前超时的剩余时间?
或者我必须在一个变量中,当设置超时时,存储当前时间,然后我们暂停,得到现在和那时之间的差异?
如果我有一个通过设置的活动超时运行var t = setTimeout("dosomething()", 5000),
无论如何要暂停和恢复它?
你可以这样包装window.setTimeout,我认为这类似于你在问题中的建议:
var Timer = function(callback, delay) {
    var timerId, start, remaining = delay;
    this.pause = function() {
        window.clearTimeout(timerId);
        remaining -= Date.now() - start;
    };
    this.resume = function() {
        start = Date.now();
        window.clearTimeout(timerId);
        timerId = window.setTimeout(callback, remaining);
    };
    this.resume();
};
var timer = new Timer(function() {
    alert("Done!");
}, 1000);
timer.pause();
// Do some stuff...
timer.resume();
像这样的事情应该可以解决问题。
function Timer(fn, countdown) {
    var ident, complete = false;
    function _time_diff(date1, date2) {
        return date2 ? date2 - date1 : new Date().getTime() - date1;
    }
    function cancel() {
        clearTimeout(ident);
    }
    function pause() {
        clearTimeout(ident);
        total_time_run = _time_diff(start_time);
        complete = total_time_run >= countdown;
    }
    function resume() {
        ident = complete ? -1 : setTimeout(fn, countdown - total_time_run);
    }
    var start_time = new Date().getTime();
    ident = setTimeout(fn, countdown);
    return { cancel: cancel, pause: pause, resume: resume };
}
不。您需要取消它 ( clearTimeout),测量自启动以来的时间并使用新时间重新启动它。
Tim Downs答案的稍微修改版本。但是,由于蒂姆回滚了我的编辑,我必须自己回答这个问题。我的解决方案可以使用 extraarguments作为第三个 (3, 4, 5 ...) 参数并清除计时器:
function Timer(callback, delay) {
    var args = arguments,
        self = this,
        timer, start;
    this.clear = function () {
        clearTimeout(timer);
    };
    this.pause = function () {
        this.clear();
        delay -= new Date() - start;
    };
    this.resume = function () {
        start = new Date();
        timer = setTimeout(function () {
            callback.apply(self, Array.prototype.slice.call(args, 2, args.length));
        }, delay);
    };
    this.resume();
}
正如 Tim 提到的,额外的参数在 中不可用IE lt 9,但是我做了一些工作,以便它也可以在 中使用oldIE。
用法: new Timer(Function, Number, arg1, arg2, arg3...)
function callback(foo, bar) {
    console.log(foo); // "foo"
    console.log(bar); // "bar"
}
var timer = new Timer(callback, 1000, "foo", "bar");
timer.pause();
document.onclick = timer.resume;
“暂停”和“继续”在 的上下文中没有多大意义setTimeout,这是一次性的事情。您可能想要暂停一系列链接的setTimeout调用,在这种情况下,不要安排下一个调用(可能通过 取消未完成的调用,clearTimeout如下所示)。但setTimeout它本身不会循环,没有什么可以暂停和恢复的。
如果你的意思是setInterval不,你不能暂停它,你只能取消它 ( clearInterval) 然后重新安排它。规范的计时器部分中所有这些的详细信息。
// Setting
var t = setInterval(doSomething, 1000);
// Pausing (which is really stopping)
clearInterval(t);
t = 0;
// Resuming (which is really just setting again)
t = setInterval(doSomething, 1000);