最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery Create Callback - Stack Overflow

programmeradmin5浏览0评论

I'd like to create a callback on a simple function.

I have this function which is called on button click:

function main(){ };

So I'd like main(), when its done to call this:

 function test(){ }

I'd like to create a callback on a simple function.

I have this function which is called on button click:

function main(){ };

So I'd like main(), when its done to call this:

 function test(){ }
Share edited Dec 15, 2011 at 7:00 Bhesh Gurung 51k23 gold badges95 silver badges143 bronze badges asked Dec 15, 2011 at 6:55 jQuerybeastjQuerybeast 14.5k39 gold badges119 silver badges198 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4
function main(callback) {
    // ... do your thing
    callback();
}

main(function(){
    alert('this is the callback speaking');
});

if the main() function not use ajax,you can use:

function main(callback) {
    // ... do your thing
    callback();
}

function test(){}

eg:
<input type="button" onclick="main(test);"/>

if the main() function use ajax,you can call test() in plete function like this:

function main(callback){
    $.ajax({
        ...
        plete: function(XMLHttpRequest, textStatus){
            callback(); 
        },
        ...
    });
}

function test(){
    ...
}
eg:
<input type="button" onclick="main(test);" value="test"/>

As i understood its simple,Sorry if i am not in the point.

function main() {
// ... do your thing
test();
}


function test() {
// ... do your thing in test

}
发布评论

评论列表(0)

  1. 暂无评论