-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
  • 131
  • 3
  • 13

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,233
  • 2
  • 12
  • 24
-1
const callbackFunction = function () { /*do something*/ };    
setInterval( callbackFunction, 1000); //callbackFunction is invoked every 1000ms (1s).
karthiks
  • 6,749
  • 6
  • 46
  • 61
Farshid Rezaei
  • 671
  • 1
  • 10
  • 32