-1

So if I had some code, and I wanted to print this certain variable every 1 second (or any number, but constantly updating) how would I do that? I need a variable to be printed onto the page every 1 second, and I may need to do similar things with other variables later, but I'm just not sure how I would do this.

Joel Banks
  • 141
  • 3
  • 14

2 Answers2

0

Set interval can do this. If you want to delay it for one time only, use setTimeout. See https://www.w3schools.com/js/js_timing.asp

let SECOND = 1000;
let variableToChange = 0;

setInterval(function(){
   variableToChange++;
}, SECOND)
Chase W.
  • 1,343
  • 2
  • 13
  • 25
-1
const callbackFunction = function () { /*do something*/ };    
setInterval( callbackFunction, 1000); //callbackFunction is invoked every 1000ms (1s).
karthiks
  • 7,049
  • 7
  • 47
  • 62
Farshid Rezaei
  • 751
  • 2
  • 10
  • 34