Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript - Do something every n seconds [duplicate]

I am fairly new to JavaScript, and I am learning it through p5 and videos by Daniel Shiffman.

I've been looking for a way to update a program every minute or so that checks a weather api, so I don't have to refresh the page myself.

I am aware that there are already answers to this question on here, but none of them make sense to me, as I am very new to JS.

So if you could answer it with an ELI5 ("Explain it like I'm five") description that would great.

like image 788
Zachary Elkins Avatar asked Nov 21 '16 00:11

Zachary Elkins


People also ask

How do you write every 5 seconds in JavaScript?

If you want to run a javascript function every 5 seconds then you can use the setInterval() method of javascript. You can pass the time in milliseconds to this function.

How can we call a function which logs a message after every 5 seconds?

You can use setInterval() , the arguments are the same.

How do you call a function every 10 seconds TypeScript?

Use the setInterval() method to call a function every N seconds in TypeScript, e.g. setInterval(myFunction, seconds * 1000) . The first parameter the method takes is the function that will be called on a timer, and the second parameter is the delay in milliseconds. Copied!


4 Answers

setInterval() is your easiest option.

Take a look at this simple example:

// Will execute myCallback every 0.5 seconds 
var intervalID = window.setInterval(myCallback, 500);

function myCallback() {
 // Your code here
}

More information and more examples can be found here: https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setInterval (https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setInterval)

like image 118
sidewalksalsa Avatar answered Sep 18 '22 15:09

sidewalksalsa


In plain vanilla Javascript, you would use setInterval:

var intervalID = window.setInterval(checkWeatherAPI, 60000);

function checkWeatherAPI() {
  // go check API 
  console.log("checking weather API");
}

If you were to run the above, the callback function: checkWeatherAPI, would run once every minute, or 60,000 milliseconds forever, full documentation here: WindwTimers.setInterval

To stop the interval you would simply run this line:

window.clearInterval(intervalID);
like image 31
Keno Avatar answered Sep 17 '22 15:09

Keno


Choosing whether setInterval OR setTimeout is based on your need and requirement as explained a bit about the difference below.

setInterval will be called irrespective of the time taken by the API. For an instance you set an API call every 5 seconds and your API call is taking 6 seconds due to the network latency or server latency, the below setInterval will trigger the second API call before the first API completes.

var timer = setInterval(callAPI, 5000);

function callAPI() {
  // TO DO
  triggerXhrRequest(function(success){

  });
}

Instead, if you want to trigger another API call after 5 seconds once the first API call completed, you can better use setTimeout as below.

var timer = setTimeout(callAPI, 5000);

function callAPI() {
  // TO DO
  triggerXhrRequest(function(success){
      timer = setTimeout(callAPI, 5000);
  });
}

setTimeout will be called once after nth seconds. So you can control when the next one can be called as above.

MDN Documentation

setTimeout

setInterval

like image 34
Aruna Avatar answered Sep 19 '22 15:09

Aruna


function doThings() {
   // The things I want to do (or increment)
}

setTimeout(doThings, 1000); // Milliseconds
like image 41
MrEhawk82 Avatar answered Sep 20 '22 15:09

MrEhawk82