gpt4 book ai didi

javascript - 当函数的工作完成时

转载 作者:数据小太阳 更新时间:2023-10-29 06:03:28 24 4
gpt4 key购买 nike

我正在使用一个 jQuery 插件,它从 url 获取数据,获取、计算并将一些数据写入 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);

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

最佳答案

你需要让 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());
});

关于javascript - 当函数的工作完成时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7155487/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com