这就是我的情况。我需要先运行这个方法
$(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/