My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

How to pass variable to function in for loop?

Anand Reddy Rikka's photo
Anand Reddy Rikka
·Jan 17, 2017

How can i get the country details into CronJob - onTick Method, It always get the last country details into the method.

for (var i = 0; i < totalCountries; i++) {
        var country = countries[i];
        var newArticleJob = new CronJob({
            cronTime: '0 */1 * * * *',
            onTick: function () {
                console.log('Job triggered to fetch new articles for timezone: ' + i);
                //firebaseDataServices.loadNewArticles();
            },
            start: false,
            timeZone: country.tz
        });
        newArticleJobs.push(newArticleJob);
    }
        for (var i = 0; i < totalCountries; i++) {
        newArticleJobs[i].start();
    }