Skip to content

Instantly share code, notes, and snippets.

@co3moz
Last active March 29, 2016 13:18
Show Gist options
  • Save co3moz/25f8ceec96457961c46a to your computer and use it in GitHub Desktop.
Save co3moz/25f8ceec96457961c46a to your computer and use it in GitHub Desktop.
Simple timeout controller for javascript, for browser and node.js
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;
}
@co3moz
Copy link
Author

co3moz commented Mar 29, 2016

example

something.onkeyup = function() {
  timeoutJob("something shouldn't work again and again", 1000, function() {
    console.log("done");
  });
};

It executes the code when client stops writing. It's good for filter mechanisms.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment