Running
`
setTimeout(function(){
window.location.reload(1);
}, 30000);
`
This gives me a refresh every 30 seconds. I want to change or pause it while user is typing into a text box.
Thinking a oninput event. How can I change the 30000?
>Solution :
One way to change the refresh interval while the user is typing in a text box would be to use the clearTimeout() function to cancel the existing timeout when the user starts typing, and then use setTimeout() again to set a new timeout when the user stops typing.
Here is an example of how you could do this:
// Set initial refresh interval
var refreshInterval = 30000; // 30 seconds
// Set initial timeout
var timeout = setTimeout(function() {
window.location.reload(1);
}, refreshInterval);
// Add event listener for the text box
document.getElementById('text-box').addEventListener('input', function() {
// Clear existing timeout when user starts typing
clearTimeout(timeout);
// Set new timeout when user stops typing
timeout = setTimeout(function() {
window.location.reload(1);
}, refreshInterval);
});
In this example, the refreshInterval variable is used to specify the refresh interval. This allows you to easily change the refresh interval without having to modify the setTimeout() call.
When the user starts typing in the text box, the clearTimeout() function is used to cancel the existing timeout. When the user stops typing, a new timeout is set using setTimeout(). This way, the page will only refresh when the user has stopped typing for the specified interval.