Skip to content

Instantly share code, notes, and snippets.

@johnloy
Last active August 27, 2021 18:54
Show Gist options
  • Save johnloy/3e054dfa523af3ca385f87afdf98c9f1 to your computer and use it in GitHub Desktop.
Save johnloy/3e054dfa523af3ca385f87afdf98c9f1 to your computer and use it in GitHub Desktop.
Timeoutify a callback. From "You Don't Know JS: Async and Performance"
function timeoutify(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, arguments );
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment