![](/img/trans.png)
[英]How do I wait for this animation to finish before moving on to the next one?
[英]How to wait for task to finish before moving on to next part of the code
我有一个“保存”按钮,当用户单击页面上的“下一个/上一个”按钮时,需要触发该按钮。 当用户单击下一步以移至另一个窗口时,我触发一个“保存”按钮,该按钮保存用户疯狂的更改和某些其他功能。 问题是,当我单击下一个或上一个按钮时,会触发保存按钮,但是需要时间来保存更改,并且代码会继续执行。 有什么方法可以告诉我保存是完成还是延迟我的代码? 我知道我可以进行时间延迟,但是对于每个用户而言,它可能会有所不同,并且效率低下。 这是一个例子:
function prev_next(){
$('#save').trigger('click'); //This line could take 2-5 seconds to finish
//Other code here that opens next window
}
任何建议将被认真考虑。
您必须编辑通过单击保存按钮触发的函数,以允许使用回调函数。 (我假设有一个ajax请求发送到某个服务器进行保存)。
例:
保存功能:
function saveEdits(callback){
$.ajax({
url: 'urltosavepage.php',
method: 'post',
data: {
somevar: 'somevalue'
},
success: function(response)
{
callback();
}
});
}
prev_next函数:
function prev_next()
{
saveEdits(function(){
// Code that is here will not get executed until after the ajax request is completed, and the success function gets called.
//Other code here that opens next window
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.