Last active
March 29, 2016 13:18
-
-
Save co3moz/25f8ceec96457961c46a to your computer and use it in GitHub Desktop.
Simple timeout controller for javascript, for browser and node.js
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
function timeoutJob(jobName, time, callback) { | |
var self = this; | |
if (timeoutJob.jobs == undefined) { | |
timeoutJob.jobs = {}; | |
} | |
if (timeoutJob.jobs[jobName] != undefined) { | |
clearTimeout(timeoutJob.jobs[jobName]); | |
} | |
timeoutJob.jobs[jobName] = setTimeout(function() { | |
delete timeoutJob.jobs[jobName]; | |
callback.call(self); | |
}, time); | |
} | |
if (typeof window !== "undefined") { | |
window.timeoutJob = timeoutJob; | |
} else if (typeof global !== "undefined") { | |
module.exports = timeoutJob; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
example
It executes the code when client stops writing. It's good for filter mechanisms.