简体   繁体   中英

Callback function is not working in javascript

I have read a few articles regarding callback function. I understand how they presented like add a + b then give callback function. But I am doing same. I first declared the function then call it again I call the callback function, why it is not working in my case?

 function me(callback){ console.log("1") } me(function(){ console.log(2) }) 

I am expecting console.log 1 then console.log 2. I am getting only console.log 1

you are calling the callback function, it won't trigger automatically, that approach is so you can notify something using that callback function when your function ended something.

 function me(callback) { console.log("1") // your process ended, lets notify callback(); } me(function() { console.log(2) }) 

You have to actually call the callback function inside the function it is passed to as argument:

 function me(callback){ console.log(1) callback(); } me(function(){ console.log(2); }) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM