找到setTimeout()中剩下的时间? [英] find the time left in a setTimeout()?

查看:132
本文介绍了找到setTimeout()中剩下的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些Javascript,它与我不拥有的库代码交互,并且不能(合理地)改变。它创建了Javascript超时,用于在一系列限时问题中显示下一个问题。这不是真正的代码,因为它超出了所有希望。这是图书馆正在做的事情:

I'm writing some Javascript that interacts with library code that I don't own, and can't (reasonably) change. It creates Javascript timeouts used for showing the next question in a series of time-limited questions. This isn't real code because it is obfuscated beyond all hope. Here's what the library is doing:

....
// setup a timeout to go to the next question based on user-supplied time
var t = questionTime * 1000
test.currentTimeout = setTimeout( showNextQuestion(questions[i+1]), t );

我想在屏幕上放置一个进度条,填充 questionTime * 1000 通过询问由 setTimeout 创建的计时器。唯一的问题是,似乎没有办法做到这一点。我缺少一个 getTimeout 函数吗?我能找到的关于Javascript超时的唯一信息仅与通过 setTimeout(函数,时间)创建,并通过 clearTimeout(id)

I want to put a progress bar onscreen that fills towards questionTime * 1000 by interrogating the timer created by setTimeout. The only problem is, there seems to be no way to do this. Is there a getTimeout function that I'm missing? The only information on Javascript timeouts that I can find is related only to creation via setTimeout( function, time) and deletion via clearTimeout( id ).

我正在寻找一个函数,它返回超时触发前的剩余时间,或者调用超时后经过的时间。我的进度条形码如下所示:

I'm looking for a function that returns either the time remaining before a timeout fires, or the time elapsed after a timeout has been called. My progress bar code looks like this:

var  timeleft = getTimeout( test.currentTimeout ); // I don't know how to do this
var  $bar = $('.control .bar');
while ( timeleft > 1 ) {
    $bar.width(timeleft / test.defaultQuestionTime * 1000);
}

tl; dr:如何在a之前找到剩余的时间javascript setTimeout()?

这是我现在使用的解决方案。我经历了负责测试的图书馆部分,并解密了代码(可怕,并且违反了我的权限)。

Here's the solution I'm using now. I went through the library section that's in charge of tests, and unscrambled the code (terrible, and against my permissions).

// setup a timeout to go to the next question based on user-supplied time
var t = questionTime * 1000
test.currentTimeout = mySetTimeout( showNextQuestion(questions[i+1]), t );

这是我的代码:

// wrapper for setTimeout
function mySetTimeout( func, timeout ) {
    timeouts[ n = setTimeout( func, timeout ) ] = {
        start: new Date().getTime(),
        end: new Date().getTime() + timeout
        t: timeout
    }
    return n;
}

这在任何不是IE 6的浏览器中都能很好地发挥作用。即使是最初的iPhone,我也希望能有所收获异步。

This works pretty spot-on in any browser that isn't IE 6. Even the original iPhone, where I expected things to get asynchronous.

推荐答案

如果您无法修改库代码,则需要重新定义setTimeout以满足您的需要。以下是您可以执行的操作的示例:

If you can't modify the library code, you'll need to redefine setTimeout to suit your purposes. Here's an example of what you could do:

(function () {
var nativeSetTimeout = window.setTimeout;

window.bindTimeout = function (listener, interval) {
    function setTimeout(code, delay) {
        var elapsed = 0,
            h;

        h = window.setInterval(function () {
                elapsed += interval;
                if (elapsed < delay) {
                    listener(delay - elapsed);
                } else {
                    window.clearInterval(h);
                }
            }, interval);
        return nativeSetTimeout(code, delay);
    }

    window.setTimeout = setTimeout;
    setTimeout._native = nativeSetTimeout;
};
}());
window.bindTimeout(function (t) {console.log(t + "ms remaining");}, 100);
window.setTimeout(function () {console.log("All done.");}, 1000);

这不是生产代码,但应该让你走上正轨。请注意,每个超时只能绑定一个侦听器。我没有对此进行过广泛的测试,但它在Firebug中有效。

This is not production code, but it should put you on the right track. Note that you can only bind one listener per timeout. I haven't done extensive testing with this, but it works in Firebug.

更强大的解决方案将使用相同的包装setTimeout技术,而是使用来自返回的timeoutId给侦听器每个超时处理多个侦听器。您也可以考虑包装clearTimeout,以便在超时被清除时分离您的侦听器。

A more robust solution would use the same technique of wrapping setTimeout, but instead use a map from the returned timeoutId to listeners to handle multiple listeners per timeout. You might also consider wrapping clearTimeout so you can detach your listener if the timeout is cleared.

这篇关于找到setTimeout()中剩下的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆