0

I've modified this existing countdown timer so that I can run multiple countdown and used session to display the timer in HTML. The runCountdown function now looks like this:

runCountdown(object, param, sessionName)

object is used instead of var myCounter of the solution. param is for countdown seconds. sessionName is for storing count current second to show it in the browser dynamically.

Now I want to run several countdown timer simultaneously , thus in the Meteor app I've done something like this

Meteor.call('doCount', function(error, result) {
  if ( !result < 1 ) {
    counters = new Array(result);
    var queryResult = ProductList.find().fetch();

    for (i = 0; i < counters.length; i++) {
      var diff = queryResult[i].expire - Math.floor(TimeSync.serverTime() / 1000);
      runCountdown(counters[i], diff, queryResult[i]._id);
    }
    console.log('testing from doCount ' + counters[0]);
  }
});

doCount returns the number of Countdown Timers I want to run in the browser.

Now the weird thing is that my runCountdown function inside the for loop is working properly as I can see from the browser console, which implies that counters variable is being used this function. Obviously my main objective is not to log the object but it's showing undefined.
Why is that happening?

The runCountdown function

function runCountdown(obj,param,sessionName){
    obj = new Countdown({
        seconds:param,  // number of seconds to count down
        onUpdateStatus:
            function(sec){
                Session.set(sessionName,sec);
                console.log(Session.get(sessionName));
            }, // callback for each second
        onCounterEnd: function(){ alert('counter ended!');} // final action
    });

    obj.start();

}
Community
  • 1
  • 1
Munna
  • 109
  • 2
  • 12

1 Answers1

0

You're expecting to store the obj object to be available outside then return the obj from the runCountdown function and store it in the counters[i] array item.

function runCountdown(param,sessionName){
obj = new Countdown({
    seconds:param,  // number of seconds to count down
    onUpdateStatus:
        function(sec){
            Session.set(sessionName,sec);
            console.log(Session.get(sessionName));
        }, // callback for each second
    onCounterEnd: function(){ alert('counter ended!');} // final action
});

obj.start();
return obj;}

now in your array structure store this object in the following way -

counters[i] = runCountdown(diff, queryResult[i]._id);
Foysal Ahamed
  • 282
  • 1
  • 7
  • 17