jQuery 延迟后运行函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10860171/
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
Run Function After Delay
提问by Joe Isaacson
I have the below global jQuery function stored, but on page load, I want to execute it after a 1000 delay. Is there something wrong with my syntax? I know the delay always goes before the function. It is not responding.
我存储了以下全局 jQuery 函数,但是在页面加载时,我想在 1000 次延迟后执行它。我的语法有问题吗?我知道延迟总是在函数之前发生。它没有响应。
Global function:
全局功能:
function showpanel() {
$(".navigation").hide();
$(".page").children(".panel").fadeIn(1000);
;}
Executing function:
执行功能:
parallax.about.onload=function(){
$('#about').delay(3000).showpanel();
};
回答by thecodeparadox
回答by ebrahim.mr
I searched and found the solution in the following URL is better.
我搜索并发现以下网址中的解决方案更好。
http://www.tutorialrepublic.com/faq/call-a-function-after-some-time-in-jquery.php
http://www.tutorialrepublic.com/faq/call-a-function-after-some-time-in-jquery.php
It worth to try.
值得一试。
It adds your given function to the queue of functions to be executed on the matched element which is currently this.
它将您给定的函数添加到要在当前this的匹配元素上执行的函数队列中。
$(this).delay(1000).queue(function() {
// your Code | Function here
$(this).dequeue();
});
and then execute the next function on the queue for the matched element(s) which is currently thisagain.
然后在队列上为匹配的元素执行下一个函数,当前再次是这个。
回答by Prince Patel
You can add timeout function in jQuery (Show alert after 3 seconds):
您可以在 jQuery 中添加超时功能(3 秒后显示警报):
$(document).ready(function($) {
setTimeout(function() {
alert("Hello");
}, 3000);
});
回答by Mohammad Kermani
This answer is just useful to understand how you can make delayusing JQuery delay
function.
此答案仅有助于了解如何使用 JQuery函数进行延迟delay
。
Imagine you have an alert and you want to set the alert text then show the alert and after a few seconds hide it.
想象一下,您有一个警报,您想设置警报文本,然后显示警报并在几秒钟后隐藏它。
Here is the simple solution:
这是简单的解决方案:
$(".alert-element").html("I'm the alert text").fadeIn(500).delay(5000).fadeOut(1000);
It is completely simple:
这很简单:
.html()
will change the text of.alert-element
.fadeIn(500)
will fade in after 500 milliseconds- JQuery
delay(5000)
function will make 5000 milliseconds of delay before calling next function .fadeOut(1000)
at the end of the statement will fade out the.alert-element
.html()
将改变文本.alert-element
.fadeIn(500)
将在 500 毫秒后淡入- JQuery
delay(5000)
函数会在调用下一个函数之前延迟 5000 毫秒 .fadeOut(1000)
在语句的末尾将淡出.alert-element