I'm quite new to javascript and I'm trying to learn time events. I'm trying to implement a function which takes a number and execute function1
for that number of times, then execute function2
. But between each execution, I'd like to have a set interval. E.g., function1
prints "foo" and function2
prints "bar". If the function's input number is "3" & interval is 5 seconds, the final printing should be foo,foo,foo,bar,foo,foo,foo,bar .... with each 5 seconds apart.
I've tried for
loop and while
loop.
var i=0;
while (i < 4){//number of times to repeat "function"
if (i%4 == 0){
setInterval(function(){
console.log("bar");
},2000);//interval between functions
}
else{
setInterval(function(){
console.log("foo");
},2000);//interval
}
i++;
if (i==4) i = 0;
}