Created
February 18, 2019 17:04
-
-
Save math314/1e4940d15160ccd67640dfa00b4d8ae5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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