Question

I would like an api call to be done immediately, then after 1sec, then 2sec, etc in incrementations of 1 sec up to 10.

I tried something like this:

Meteor.startup ->
  counter = 1000
  Meteor.setInterval (->
    Meteor.call "call_url", url, (err, result) ->
        ...

    if counter < 10000
      counter += 1000
      console.log counter
  ), counter

While my counter is incremented, the log is done exactly every seconds, which means that the setInterval doesn't keep track of the value.

The only way I see to handle this would be having 9 setTimeout to call the api at different times, then a Meteor.setInterval starting after all the timeouts... Sounds very ugly.

Any suggestions on how to do this in a clean way? It's important for the user to see frequent updates when he just connects to the page, but if he decides to let it open for a while, there is no need to perform that api query so often.

Was it helpful?

Solution

A more general solution (and scalable in the future for CoinsManager) is to use a queuing package that supports scheduling events in the future. I've looked at a bunch of background task management for Meteor, and queue supports scheduling.

OTHER TIPS

Not quite clear what you are asking and there are probably a lot of ways to generate the intervals you want.

In javascript it might look like this:

var doStuff = function(){...};
var intervals = [ 1000, 3000, 6000, 10000, 15000, 21000, 28000, 36000, 45000, 55000 ];

doStuff();  //run immediately
intervals.forEach( function( interval ){
  Meteor.setTimeout( doStuff, interval );
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top