Passing anonymous function as callback in Javascript

那年仲夏 提交于 2019-12-05 12:05:19

Your code pattern should be like

function notifyme(msg,callback){
    console.log(msg);
    // Do your stuff here
    if(callback){
      callback();
    }
}

notifyme("msg", function(){
    //do some custom redirect logic    
});

You need to call your callback!

function notifyme(msg, callback){
    console.log(msg);
    callback();
}

notifyme("msg", function(){
//do some custom redirect logic    
});

You can call your function simply by invoking it ():

function notifyme(msg, myFunc){
        console.log(msg);
        myFunc();
}
    
notifyme("msg", function(){
        console.log("function called");   
});

myFunc in the above is a variable pointing at your function. By invoking it, () you call the function that the variable (myFunc) points at.

This answer doesn't go into details about the custom redirect logic. Because I don't know what this does.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!