具有随机recurTime的TimerManager

| 我希望每个迭代都有一个随机的到期时间。 本示例将仅将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     
已邀请:
问题在于,TimerManager.start的
recurTime
参数是普通函数的普通参数,因此在调用该函数时只对它求值一次。这不是一个一遍又一遍地被重新评估的表达式。这意味着您只能使用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);
    

要回复问题请先登录注册