当一个功能的工作完成

mrd*_*iri 5 javascript jquery

我正在使用jQuery插件,它从网址获取数据,提取,计算和写入一些数据div.

我希望将这些div内容复制到另一个div,当这些功能完成它的工作时.

例如:

$("#div1").myfunction(); // it gets and calculates data and adds to #div1 . it needs 2-3 seconds to be done
var contents = $("#div1").html(); // when myfunction() done, copy contents
$("#div2").html(contents);
Run Code Online (Sandbox Code Playgroud)

当我运行该代码时,我没有新的内容#div2.

Ili*_*oly 5

您需要myfunction获取一个请求完成后执行的回调参数

function myfunction(cb)
    $.ajax({
        type: "get",
        url: "page.html",
        success: function(data){
            $("#div1").html(data);
            if(typeof cb === 'function') cb();
        }
    });
}

myfunction(function(){
    $("#div2").html($("#div1").html());
});
Run Code Online (Sandbox Code Playgroud)