Something Like that:
Closures and CallBack
For that you need to create first html code.
HTML
<p id="demo"></p>
After that add this JS code.
JS
function sayHiLater(){ var greeting = 'Hi!'; setTimeout(function(){ document.getElementById('demo').innerHTML = greeting; }, 3000); } sayHiLater();
[codepen_embed height=”265″ theme_id=”0″ slug_hash=”KvZoxx” default_tab=”result” user=”pradeepanvi”]See the Pen Closures and CallBack in javascript by Pradeep Kumar (@pradeepanvi) on CodePen.[/codepen_embed]
Now you can see
Hi!
CallBack Function
A Function you give to another function, to be run when the other function is finished
(So the function you call (i.e. invoke), ‘calls back’ by calling the function you gave it when it finishes.)
For that you need to create first html code.
HTML
<p id="demo1"></p> <p id="demo2"></p>
After that add this JS code.
JS
function tellMeWhenDone(callback){ var a = 1000; //some work var b = 2000; //some work callback(); // the 'callback', it runs the function I give it! } tellMeWhenDone(function(){ document.getElementById('demo1').innerHTML = 'I am done!'; }); tellMeWhenDone(function(){ document.getElementById('demo2').innerHTML = 'All done!'; });
[codepen_embed height=”265″ theme_id=”0″ slug_hash=”bravOv” default_tab=”result” user=”pradeepanvi”]See the Pen CallBack Function in javascript by Pradeep Kumar (@pradeepanvi) on CodePen.[/codepen_embed]
Now you can see
I am done!
All done!