How to call a function every hour?

后端 未结 5 1017
轻奢々
轻奢々 2021-02-07 09:30

I am trying to update information from a weather service on my page. The info should be updated every hour on the hour. How exactly do I go about calling a function on the hour

相关标签:
5条回答
  • 2021-02-07 09:57

    What you probably want is something like that:

    var now = new Date();
    var delay = 60 * 60 * 1000; // 1 hour in msec
    var start = delay - (now.getMinutes() * 60 + now.getSeconds()) * 1000 + now.getMilliseconds();
    
    setTimeout(function doSomething() {
       // do the operation
       // ... your code here...
    
       // schedule the next tick
       setTimeout(doSomething, delay);
    }, start);
    

    So basically the first time the user get the access, you need to know what is the delay in millisecond to the next "hour". So, if the user access to the page at 8:54 (with 56 seconds and 123 milliseconds), you have to schedule the first execution after around 3 minutes: after the first one is done, you can call it every "hour" (60 * 60 * 1000).

    0 讨论(0)
  • 2021-02-07 10:10

    EDIT: Oops, I didn't see the " o' clock" things, so I edit my answer :

    var last_execution = new Date().getTime();
    function doSomething(force){
      var current_time = new Date().getTime();
      if (force || (current_time.getMinutes() == 0)
      {
        last_execution = current_time;
        // something
        // ...
      }
      setTimeout(doSomething(false), 1000);
    }
    // force the first time
    doSomething(true); 
    
    0 讨论(0)
  • 2021-02-07 10:16
    // ... call your func now
    let intervalId;
    let timeoutId = setTimeout(() => {
      // ... call your func on end of current hour
      intervalId = setInterval(() => {
         // ... call your func on end of each next hours
      }, 3600000);
    }, ((60 − moment().minutes()) × 60 × 1000) - (moment().second() * 1000));
    
    0 讨论(0)
  • 2021-02-07 10:17

    Here is what should work (JSFiddle):

    function tick() {
      //get the mins of the current time
      var mins = new Date().getMinutes();
      if (mins == "00") {
        alert('Do stuff');
      }
      console.log('Tick ' + mins);
    }
    
    setInterval(tick, 1000);

    0 讨论(0)
  • 2021-02-07 10:18

    You want to check out setInterval: https://developer.mozilla.org/en-US/docs/Web/API/Window.setInterval

    It's a little hard to tell what you're trying to call with your code, but it would be something in the form of:

    function callEveryHour() {
        setInterval(yourFunction, 1000 * 60 * 60);
    }
    

    If you want it every hour, try something like:

    var nextDate = new Date();
    if (nextDate.getMinutes() === 0) { // You can check for seconds here too
        callEveryHour()
    } else {
        nextDate.setHours(nextDate.getHours() + 1);
        nextDate.setMinutes(0);
        nextDate.setSeconds(0);// I wouldn't do milliseconds too ;)
    
        var difference = nextDate - new Date();
        setTimeout(callEveryHour, difference);
    }
    

    Now, this implementation checks the time once, sets the delay (or calls the function immediately), and then relies on setInterval to keep track after that. An alternative approach may be to poll the time every x many seconds/minutes, and fire it .getMinutes() == 0 instead (similar to the first part of the if-statement), which may sacrifice (marginal) performance for (marginal) accuracy. Depending on your exact needs, I would play around with both solutions.

    0 讨论(0)
提交回复
热议问题