user5210131
user5210131

Reputation:

call a function on specific time(and date)

i am developing an app with jQuery mobile and cordova and i have a countdown in my app(which is a plugin added to my project), and i want when this timer reaches 10 seconds, a specific function be called.the first option is my countdown be checked every second, but it is impossible do to some performance issues and that's not really reasonable because my countdown may show 5 days. another option is to use setTimeout, but i think that would decrease performance. wont it? assumesetTimeout(function{//function call}, 14400000) just for 4 hours let alone days.

if it does decrease the performance, what other options do i have? is there anything like crone in node.js for javascript or jQuery, so that we can specify a time and date for function to be called?

i mean that would be fine if i can set time and date for function call for example on 2016-09-01 15:10:40

tnx

Upvotes: 3

Views: 2046

Answers (2)

Adrian Theodorescu
Adrian Theodorescu

Reputation: 12327

There is only a negligible performance penalty for calling setTimeout. The best way to solve the problem you described is to calculate the number of milliseconds between the current time and the time you want to execute the code at and call setTimeout using that value as timeout. However, there's a gotcha that yo have to pay attention to, as outlined by @Vld: the delay parameter is stored as a 32 bit integer by some browsers, so we need to work around that and re-create the timeout periodically until we reach the date we want.

Also, you have to be aware that the date you use for the futureDate parameter in the sample code is in UTC timezone, so make sure to convert your local time to UTC before using it. A good library yo can use for that is Moment.js.

Here's the code:

function scheduleExecution(futureDate, callback) {
    // Set an intermediary timeout at every 1 hour interval, to avoid the
    // 32 bit limitation in setting the timeout delay
    var maxInterval = 60 * 60 * 1000;
    var now = new Date();

    if ((futureDate - now) > maxInterval) {
        // Wait for maxInterval milliseconds, but make
        // sure we don't go over the scheduled date
        setTimeout(
            function() { scheduleExecution(futureDate); },
            Math.min(futureDate - now, maxInterval));
    } else {
        // Set final timeout
        setTimeout(callback, futureDate - now);
    }
}

// This example uses time zone UTC-5. Make sure to use the
// correct offset for your local time zone
var futureDate = new Date("2020-09-01T17:30:10-05:00");
scheduleExecution(futureDate, function() {
    alert('boom!');
});

Upvotes: 1

Slavik Meltser
Slavik Meltser

Reputation: 10361

  1. There is no significant performance issue with running a script every second, as long as the script that is executed is as simple as check if it reached the desire time.
  2. I don't see any performance issues with using setTimeout, you can set it as far as you like and it'll absolutely not impact on the performance.

Upvotes: 0

Related Questions