了解回调

I've followed this explanation here, but am still unsure how callbacks work.

I do my call here:

scene.foo(scene.myCallBack("mymap"));

And my callback stuff here:

1) scene.foo calls foo and passes in CallBack function with "mymap" JSON data

2) foo's $.ajax defines its success as the passed in CallBack. Once the data is processed, success calls the callback, myCallBack()

3) myCallBack attempts to getJSON from the file name and console.log it as a string with JSON.stringfy

Am I not understanding this correctly?

foo : function (callback) {
    $.ajax({
        success: callback //once AJAX request is successfull, $.ajax will call callback (myCallBack) and pass response back to callback
    });
},
myCallBack : function (name) {
    $.getJSON("C:/Users/danniu/Desktop/JavaScript/tilemap/maps/" + name + ".json", function (data) {
        var myJSONText = JSON.stringify(data);
        console.log("json: " + this.myJSONText);        
    });
},
查看全部
weixin_33739523
weixin_33739523
2014/02/26 01:40
  • json
  • callback
  • ajax
  • javascript
  • jquery
  • 点赞
  • 收藏
  • 回答
    私信

2个回复