gpt4 book ai didi

jQuery,等待方法完成然后触发操作

转载 作者:行者123 更新时间:2023-12-01 08:35:54 24 4
gpt4 key购买 nike

这就是我的情况。我需要先运行这个方法

$(editor.image.toggleCaption);

然后:

alert('hello world');

如果我这样做:

$(editor.image.toggleCaption);
alert('hello world');

它不起作用,因为警报在该方法完成之前被触发。

我可以让它工作的唯一方法是使用 setTimeout。但这是一个静态解决方案,所以我想避免它。我尝试了 $.Deferred 但我不知道如何设置它,并且我尝试的所有组合都不起作用。

有人可以帮助我吗?

提前致谢

最佳答案

使用when,这似乎有效。ref https://api.jquery.com/jquery.when/ .将 $('.test').hide('slow') 替换为您的函数。希望这会有所帮助

$.when($('.test').hide('slow')).then(function(){
alert(1);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="test">Some Contents</div>

关于jQuery,等待方法完成然后触发操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55645345/

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