-
-
Save kolber/507535 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
Delay function call by a specified time | |
--------------------------------------------- | |
If the method is called again before the delay is up, reset the delay. | |
An optional id can be passed to prevent timeout collisions. | |
*/ | |
Function.prototype.sleep = function(ms, id) { | |
var id = id || 0; | |
!window.sleep_delay && (window.sleep_delay = []); | |
!!window.sleep_delay[id] && clearTimeout(window.sleep_delay[id]); | |
window.sleep_delay[id] = setTimeout(this, ms, id); | |
}; | |
/* | |
Usage | |
--------------------------------------------- | |
<!DOCTYPE html> | |
<html lang="en"> | |
<input type="text" name="name1" id="input1"> | |
<input type="text" name="name2" id="input2"> | |
<script src="./sleep.js"></script> | |
</html> | |
--------------------------------------------- | |
document.getElementById('input1').onkeyup = | |
document.getElementById('input2').onkeyup = function() { | |
(function(id) { | |
console.log('Finished typing in "'+id+'"'); | |
}).sleep(800, this.id); | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment