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:

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

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

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

我想在屏幕上放置一个进度条,通过询问setTimeout创建的计时器来填充questionTime * 1000。唯一的问题是,似乎没有办法做到这一点。有一个getTimeout函数我漏掉了吗?我能找到的关于Javascript超时的唯一信息只与通过setTimeout(函数,时间)创建和通过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: How do I find the time remaining before a javascript setTimeout()?

tl;dr:如何找到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).

这是我现在用的方法。我浏览了负责测试的library部分,并对代码进行了解密(很糟糕,而且违反了我的权限)。

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

and here's my code:

这是我的代码:

// 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;
}

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

这在IE 6之外的任何浏览器中都很有效。甚至是最初的iPhone,我希望它能实现异步。

10 个解决方案

#1


24

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:

如果不能修改库代码,则需要重新定义setTimeout以适应您的目的。这里有一个你可以做的例子:

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

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.

这不是生产代码,但它应该使您走上正确的轨道。注意,每次超时只能绑定一个侦听器。我还没有做过大量的测试,但是它在Firebug中工作。

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的相同技术,而是使用从返回的timeoutId到侦听器的映射来处理每个超时的多个侦听器。您还可以考虑包装clearTimeout,以便在超时被清除时可以分离侦听器。

更多相关文章

  1. 计算机视觉相关代码片段(Python)
  2. 120行python代码解锁10000分微信跳一跳
  3. XGBoost中参数调优的完整指南(含Python-3.X代码)
  4. 求助:Python是否可以用一行代码来同时给变量赋值并打印变量的值
  5. 长安铃木经销商爬取(解析xml、post提交、python中使用js代码)
  6. 怎样写贪吃蛇小游戏?用100行python代码轻松解决!
  7. 建模分析之机器学习算法(附python&R代码)
  8. 读取python中的unicode文件,该文件以与python源代码相同的方式声
  9. 自动完成在VS代码和Python中的自动化对象

随机推荐

  1. android 学习电子书——下载
  2. android调用Google搜索框
  3. Ubuntu 12.10 搭建 Eclipse Android(安卓
  4. android 3.0编译环境需要的所有组件
  5. Android文件保存和读取
  6. android 中添加 admob 广告
  7. android用代码实现圆角背景
  8. Android Google App
  9. android 蓝牙各种UUID
  10. 3、android颜色取值