Skip to content

Instantly share code, notes, and snippets.

@cladley
Created June 27, 2016 15:26
Show Gist options
  • Save cladley/80b93cb0286548d4e14c1c9495276a4a to your computer and use it in GitHub Desktop.
Save cladley/80b93cb0286548d4e14c1c9495276a4a to your computer and use it in GitHub Desktop.
function timeoutfunc(fn,delay) {
var intv = setTimeout(function() {
intv = null;
fn(new Error("Timeout"));
}, delay);
return function() {
// timeout hasn't happened yet?
if(intv) {
clearTimeout(intv);
fn.apply(this, [].concat([].slice.call(arguments)));
}
};
}
// Dummy ajax call
var ajax = function(url, callback){
setTimeout(function(){
callback("From inside ajax");
}, 4000);
};
function myCallback(message){
console.log(message);
}
// If the callback doesnt return within 5000, call the
// callback with error object
ajax('http://somewhere.com', timeoutfunc(myCallback, 5000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment