Something Like that:
CallBack function
For that you need to create first html code.
HTML
<p>Value of function wait3s = <span id="demo1"></span></p> <p>Value of function clickH = <span id="demo2"></span></p> <p>Value of finished = <span id="demo3"></span></p>
After that add this JS code.
JS
/*CallBack*/
//long running function
function wait3s(){
var ms = 3000 + new Date().getTime();
while (new Date() < ms){}
document.getElementById("demo1").innerHTML = "finished function";
}
function clickH(){
document.getElementById("demo2").innerHTML = "clicked";
}
//listen for the click event
document.addEventListener('click', clickH);
wait3s();
document.getElementById("demo3").innerHTML = "finished execution";
/*CallBack End*/
[codepen_embed height="265" theme_id="0" slug_hash="qXbzxr" default_tab="result" user="pradeepanvi"]See the Pen CallBack function in javascript by Pradeep Kumar (@pradeepanvi) on CodePen.[/codepen_embed]
Now you can see after 3 second.
Value of function wait3s = finished function
Value of function clickH =
Value of finished = finished execution
And on click document anywhere
Value of function wait3s = finished function
Value of function clickH = clicked
Value of finished = finished execution