开发者

TimerManager with random recurTime

开发者 https://www.devze.com 2023-03-03 21:05 出处:网络
I want a random expiration time on EACH iteration. This example will only randomize an expiration time between 5~15 seconds and use them forever.

I want a random expiration time on EACH iteration. This example will only randomize an expiration time between 5~15 seconds and use them forever.

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

I also accept an pure JS solution if any.

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


The issue is that the recurTime argument to TimerManager.start is a normal argument to a normal function, so it is evaluated only once when the function is called. It's not an expression that gets re-evaluated over and over again. That means you only get equidistant executions with TimerManager.

You probably have to code what you want by hand, e.g. using qx.event.Timer.once calculating the timeout anew with each invocation.

EDIT:

Here is a code snippet that might go in the right direction for you (this would work in the context of a qooxdoo class):

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

精彩评论

暂无评论...
验证码 换一张
取 消