Skip to content

Instantly share code, notes, and snippets.

@alanland
Created July 24, 2013 06:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanland/6068467 to your computer and use it in GitHub Desktop.
Save alanland/6068467 to your computer and use it in GitHub Desktop.
Javascript 延迟
//Call a javascript function after 5 sec of last key press
var timeout;
$(document).ready(function(){
$('input[type=text]').keypress(function() {
if(timeout) {
clearTimeout(timeout);
timeout = null;
}
timeout = setTimeout(myFunction, 5000);
});
});
var myFunction = new function() {
alert('myFunction is running');
clearTimeout(timeout); // this way will not run infinitely
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment