javascript - SetTimeout not functioning properly -


this code running 21 console logs @ once. should, however, run them 1 @ time @ set interval suggestions?

var index = 1; var switchbg = function(num){     if( num < 22 ){         console.log('index' + index);         console.log('num' + num);         index++;         function_timer(index);     } };  var timer; var function_timer = function(index){     cleartimeout(timer);     timer = settimeout(switchbg(index), 10000);  }; 

you need pass function argument settimeout. try this:

timer = settimeout(function() {     switchbg(index); }, 10000); 

doing settimeout(switchbg(index), 10000); evaluates switchbg(index) , passes return value (which undefined) settimeout.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -