3

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

  • If you make an interval tick every 1s, (talking front-end here) what performance would possibly concern you? – Roko C. Buljan Sep 01 '16 at 19:34
  • Also, why don't you do like: (pseudo) *if is right date > check time > if right time > do stuff*. If an exact date is your concern you should not ever use clients datetime cause it can be faked changing OS date-time. – Roko C. Buljan Sep 01 '16 at 19:36

2 Answers2

1

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!');
});
Adrian Theodorescu
  • 11,664
  • 2
  • 23
  • 30
  • Your posted example won't actually work. The maximum time in advance you can set is [2147483647 milliseconds](http://stackoverflow.com/questions/39007056/is-there-a-maximum-delay-limit-for-window-setinterval/39007143#39007143) which is roughly about 24 days. So, if you want to set it for "far" in the future, make sure it's within about three weeks-three weeks and a half. – VLAZ Sep 01 '16 at 19:44
  • I don't think that's true. The delay parameter of setTimeout doesn't seem to have any restrictions (see https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setTimeout), so you can set it to the maximum number value supported in JavaScript, which is 1.7976931348623157e+308. That should take you far enough in time. – Adrian Theodorescu Sep 01 '16 at 19:52
  • The text I have in my linked answer is literally from the article you are now linking me to. Check the very bottom of the page under the heading "Maximum delay value". – VLAZ Sep 01 '16 at 19:54
  • @Vld: I missed that section in the reference and I incorrectly assumed that delay is a JS number type. Apologies for that. I updated my answer to account for this limitation. – Adrian Theodorescu Sep 01 '16 at 20:33
0
  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.
Slavik Meltser
  • 9,712
  • 3
  • 47
  • 48