jQuery — 使用 .delay() 来推迟 .load()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4881666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery — Using .delay() to postpone a .load()?
提问by AJB
I've just run into an issue with using jQuery's .delay() method (v 1.4.2) to try to delay a following .load() method.
我刚刚遇到了使用 jQuery 的 .delay() 方法 (v 1.4.2) 尝试延迟后续 .load() 方法的问题。
Like so:
像这样:
$('myElement').delay(500).load('myPage.php');
It seems to just ignore the .delay().
它似乎只是忽略了 .delay()。
I've evenutally figured out a work-around like so:
我最终想出了一个解决方法:
setTimeout(function(){
$('myElement').load('myPage.php');
}, 500);
But, that's not as elegant. Anyone else run into this and found a better solution?
但是,这并不优雅。还有其他人遇到过这个问题并找到了更好的解决方案吗?
Thanks.
谢谢。
回答by user113716
Yes, the the delay()
(docs)method will be ignored, because the load()
(docs)method is not automatically queued.
是的,delay()
(docs)方法将被忽略,因为load()
(docs)方法不会自动排队。
The setTimeout
is probably your best option, though you could technically accomplish it with .delay()
if you add the load()
call to the queue using the queue()
(docs)method.
这setTimeout
可能是您最好的选择,但.delay()
如果您load()
使用queue()
(docs)方法将调用添加到队列中,您可以在技术上完成它。
$('myElement').delay(500).queue(function( nxt ) {
$(this).load('myPage.php');
nxt();
});
The function is added to the queue. You need to then call the parameter of the function to free up the queue. There's a method called the dequeue()
(docs)method that can accomplish this as well.
该函数被添加到队列中。然后您需要调用该函数的参数来释放队列。有一种称为dequeue()
(docs)方法的方法也可以完成此操作。
回答by Laurens Swart
Here's my solution to fading out a div, then loading new content into it, and then fading it back in. This is really usefull for AJAX calls to load search results for example.
这是我淡出 div,然后将新内容加载到其中,然后将其淡入的解决方案。例如,这对于加载搜索结果的 AJAX 调用非常有用。
$('.search').keyup(function(){
var query= $(this).val();
$('.results').fadeOut('300');
setTimeout(function(){
$('.results').load('search.php?search=' + query, function() {
$('.results').fadeIn('300');
})
}, 300);
return false;
});
回答by Petah
If you use the jQuery timers plugin you can do
如果您使用 jQuery 计时器插件,您可以
$('myElement').oneTime(500, function() {
$(this).load('myPage.php');
});