0

我想要每次迭代的随机到期时间。这个例子只会在 5~15 秒之间随机化一个过期时间并永远使用它们。

var timer = qx.util.TimerManager.getInstance();
timer.start(function(userData, timerId)
    {
        this.debug("timer tick");
    },
    (Math.floor(Math.random()*11)*1000) + 5000,
    this,
    null,
    0
);

如果有的话,我也接受纯 JS 解决方案。

http://demo.qooxdoo.org/current/apiviewer/#qx.util.TimerManager

4

1 回答 1

1

问题是recurTimeTimerManager.start 的参数是普通函数的普通参数,因此在调用函数时只计算一次。这不是一个被一遍又一遍地重新评估的表达式。这意味着您只能使用 TimerManager 获得等距执行。

您可能必须手动编写您想要的代码,例如使用qx.event.Timer.once每次调用重新计算超时。

编辑:

这是一个可能对您来说朝着正确方向发展的代码片段(这将在 qooxdoo 类的上下文中工作):

var that = this;
function doStuff(timeout) {
  // do the things here you want to do in every timeout
  // this example just logs the new calculated time offset
  that.debug(timeout);
}

function callBack() {
  // this just calls doStuff and handles a new time offset
  var timeout = (Math.floor(Math.random()*11)*1000) + 5000;
  doStuff(timeout);
  qx.event.Timer.once(callBack, that, timeout);
}

// fire off the first execution
qx.event.Timer.once(callBack, that, 5000);
于 2011-05-08T08:52:01.827 回答