Skip to content

Instantly share code, notes, and snippets.

@math314
Created February 18, 2019 17:04
Show Gist options
  • Save math314/1e4940d15160ccd67640dfa00b4d8ae5 to your computer and use it in GitHub Desktop.
Save math314/1e4940d15160ccd67640dfa00b4d8ae5 to your computer and use it in GitHub Desktop.
// https://jsfiddle.net/bxqfL89j/1/
/*
<div id="container">
<button>Click To Show Message</button>
</div>
*/
function callFnWithTimeout(fn, timeoutMilli) {
const waitPromise = new Promise(resolve => setTimeout(resolve, timeoutMilli));
let outsideResolve;
const controlablePromise = new Promise(function(resolve) {
outsideResolve = resolve;
});
Promise.race([waitPromise, controlablePromise])
.then(fn);
return outsideResolve;
}
$("button").click(callFnWithTimeout(() => {
$('#container').append('<p id="message">hello</p>');
}, 2000));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment