callback function meaning

后端 未结 4 769
梦谈多话
梦谈多话 2020-12-03 06:06

What is the meaning of callback function in javascript.

4条回答
  •  一个人的身影
    2020-12-03 06:57

    It's just a name for a function that should be called back after something.

    It's often used with XMLHttpRequest:

    var x = new XMLHttpRequest();
    x.onreadystatechange = function(){
        if(x.readyState == 4){
            callbackfunction(x.responseText);
        }
    }
    x.open('get', 'http://example.com/', true);
    x.send(null);
    

    callbackfunction is just a plain function, in this case:

    function callbackfunction(text){
        alert("I received: " + text);
    }
    

提交回复
热议问题