How to call a function every hour?

后端 未结 5 1018
轻奢々
轻奢々 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 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));
    

提交回复
热议问题