简体   繁体   English

如何推迟jQuery中的函数执行

[英]How to defer function execution in jQuery

I am insanely confused today by all of the code available on deferring jquery execution. 今天,我对延缓jquery执行的所有可用代码感到非常困惑。

Right now I have this written 现在我写了

 $('#filterTheTable').click(function () {
                        $('#waitingGif').show('fast')
                                 .promise()
                                 .done(FilterTable);

So the GIF shows up, the table filters itself after a couple seconds, and all is good (except I haven't hidden the GIF. 这样就会显示GIF,表格会在几秒钟后进行自我过滤,一切都很好(除非我没有隐藏GIF。

So I throw in this... 所以我扔了这个...

 $('#filterTheTable').click(function () {
                        $('#waitingGif').show('fast')
                                 .promise()
                                 .done(FilterTable);
 $('#waitingGif').hide('fast');

And as many of you probably see already, what happens is the gif flashes for a second, and the FilterTable function continues to execute. 正如您中许多人可能已经看到的那样,发生的情况是gif闪烁了一秒钟,并且FilterTable函数继续执行。

I have read a few things but can't seem to wrap my head around deferred functions. 我已经阅读了一些内容,但似乎无法绕过延迟函数。 So I gave up and put the hide('fast') code in FilterTable but now the GIF freezes while the function executes. 所以我放弃了hide('fast')代码FilterTable其放在FilterTable但是现在GIF在函数执行时冻结了。

Then I figured oh maybe I can just call promise again! 然后我想到了哦,也许我可以再打电话给promise!

 $('#filterTheTable').click(function () {
                        $('#waitingGif').show('fast')
                                 .promise()
                                 .done(FilterTable)
                                 .promise()
                                 .done(Function(){$('#waitingGif').hide('fast')});

But I get an error that promise isn't supported on that object (I assume now that .Done() is not returning the right type) 但是我收到一个错误,该对象不支持promise(我现在假设.Done()没有返回正确的类型)

I know this has to be easy to do... but I just don't seem to have my mind wrapped around this. 我知道这很容易做到...但是我似乎并没有为此而烦恼。 Are there any good posts / solutions that can get me back on the rails? 有什么好的帖子/解决方案可以使我重回正轨吗?

Thanks a million everyone. 谢谢大家一百万

So I found the answer. 所以我找到了答案。 Basically I was doing in right when I was doing ShowGif.Promise.Done(Function) and then within Function hiding the gif. 基本上我在做ShowGif.Promise.Done(Function)时做的正确,然后在Function隐藏gif。 The answer to the gif freezing is in this great answer. gif冻结的答案就是这个好答案。

In summary, DOM heavy operation's don't play nice with your GIF animation. 总之,DOM重操作对您的GIF动画效果不佳。 You can split the function up so that it emulates multi threading. 您可以拆分该函数,使其模拟多线程。 To do that you just need to split up the function and let the DOM breathe 为此,您只需要拆分功能并让DOM 呼吸

Thanks everyone. 感谢大家。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM