Created
June 27, 2016 15:26
-
-
Save cladley/80b93cb0286548d4e14c1c9495276a4a to your computer and use it in GitHub Desktop.
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
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