开发者

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

开发者 https://www.devze.com 2023-03-30 08:44 出处:网络
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 -

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 HTM开发者_运维百科L 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..


var myTimeout = setTimeout ( ...)

to clear it simply write clearTimeout(myTimeout);

0

精彩评论

暂无评论...
验证码 换一张
取 消