for (i=0;i<5;i++){
setTimeout(bye,2000)}
console.log("asda")
function bye(){
console.log("gudbye")
}
I want the program to log bye once evey 2 second,but this works just for the fist log, so i get instantly 5 time "gudbye" in 2 seconds
for (i=0;i<5;i++){
setTimeout(bye,2000)}
console.log("asda")
function bye(){
console.log("gudbye")
}
I want the program to log bye once evey 2 second,but this works just for the fist log, so i get instantly 5 time "gudbye" in 2 seconds
Change your code to:
setInterval(bye,2000)
function bye(){
console.log("gudbye")
}
setInterval will run your function every 2 seconds, instead of running just one time after 2000 seconds per call.
but if you only want it to be called 5 times you can try the recursive aproach:
bye(1)
function bye(callCount){
console.log("gudbye")
if(callCount < 5){
setTimeout(() => bye(callCount+1), 2000);
}
}