Created
July 24, 2013 06:35
-
-
Save alanland/6068467 to your computer and use it in GitHub Desktop.
Javascript 延迟
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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