Reputation: 595
lets say a website receives some data from users continuously. I want it to alert()
users if it haven't received data from users in as long as 5 seconds.
this is How I'm trying to do it:
countDown = 5000
if(//some data received from user){
countDown = 5000
}
setTimeout(
function() {
alert('no data in 5 seconds')
}, countDown);
}
well it's not working because It can't set countDown to 5 sec every time It receives data from users, So How can I do this?
and since the data from user comes continuously I guess it wouldn't be good for performance to run a new setTimeout()
and destroy the old one every time.(?)
Upvotes: 0
Views: 66
Reputation: 115
This would be more of a recursive function that checks if there is any data. If not, it makes a function that runs in five seconds. If the data is still not there, you repeat the process.
let countDown = 5000
function someFunction() {
//this is called when you recieve data
timeoutFunc();
}
function timeoutFunc() {
setTimeout(
function() {
if (//still don't have new data) {
alert('no data in 5 seconds');
timeoutFunc();
}
}, countDown);
}
}
Upvotes: 1