Timers/setInterval: resetting the time interval from inside of the function? (in React)


Timers/setInterval: resetting the time interval from inside of the function? (in React)

I'm trying to get a timer to reset its time interval from inside the function based on some props (I'm using React), but it's not working.

Here's a JSFiddle. I clearly don't understand how these functions handle scope.

http://ift.tt/2uWoIrZ

var time = 1000; 

setInterval(function(){ time = Math.random()*1000; console.log("hi"); },time);

It should randomize the console.log, but it still stays at 1000ms on each iteration.

Submitted August 02, 2017 at 11:11PM by tangerto
via reddit http://ift.tt/2viGVRl

Advertisements