Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset the time value of setTimeout() in Javascript?

I want to display an alert() box, just 10 seconds after user stops writting in a textbox.

I used following javascript code to open alertbox -

function txtkeyup(e){
  setTimeout(function () {
    alert('submit?');
  }, 10000);
  return true;
}

and the HTML code is -

<input type='textbox' name='searchquery' value='' onkeyup='return txtkeyup();'>

Now the browser is giving alertbox, 10 seconds after every onkeyup event in the inputbox. To make only one request, i have to reset the setTimeout() timer on every keyup event so the alertbox will be display if user doesnt press a button for 10 seconds.

How can reset the timer of previously called 'setTimeout()' in javascript? Please guide me..

like image 728
Vinay Jeurkar Avatar asked Aug 25 '11 07:08

Vinay Jeurkar


People also ask

How do I reset setTimeout?

Use the clearTimeout Function We can use the timer created by setTimeout function with the clearTimeout function. We have the timer variable that stores the timer returned by setTimeout .

How do I reset setTimeout react?

To clear a timeout or an interval in React with hooks: Use the useEffect hook to set up the timeout or interval. Return a function from the useEffect hook. Use the clearTimeout() or clearInterval() methods to remove the timeout when the component unmounts.

Which function is used to reset the timeout clock?

The clearTimeout() method clears a timer set with the setTimeout() method.


1 Answers

var myTimeout = setTimeout ( ...)

to clear it simply write clearTimeout(myTimeout);

like image 52
Thomas Avatar answered Sep 21 '22 04:09

Thomas