javascript 在 jQuery 中,如何每 5 秒创建一次警报?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4653203/
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
In jQuery how do I create alert every 5 seconds?
提问by TIMEX
Can I do this using jQuery or should I be looking at using something else?
我可以使用 jQuery 来做到这一点,还是应该考虑使用其他东西?
回答by Highway of Life
The jQuery.delay()function is designed to delay execution of functions in the jQuery queue.
There is already a built-in Javascript function for handling interval functionality, you wouldn't use jQuery to achieve that.
该jQuery.delay()函数旨在延迟 jQuery 队列中函数的执行。已经有一个用于处理间隔功能的内置 Javascript 函数,您不会使用 jQuery 来实现它。
setInterval(function() {
alert("Message to alert every 5 seconds");
}, 5000);
回答by Jacob Relkin
Again, no need for jQuery here:
同样,这里不需要 jQuery:
setInterval(function() {
alert("How Now Brown Cow");
}, 5000);
(Don't tell anyone, but @Justin Niessner is right, jQuery isJavaScript)
(不要告诉任何人,但@Justin Niessner 是对的,jQuery是JavaScript)
回答by Sam Dufel
You can either use setInterval()or setTimeout(). There may be a jQuery specific helper function as well.
您可以使用setInterval()或setTimeout()。也可能有一个 jQuery 特定的辅助函数。
setInterval()takes 2 parameters - a function to execute, and the delay in milliseconds between calls. It will keep calling the function until you change or pages or call clearInterval(intervalID). You have to pass in the value returned by setInterval()to clear it.
setInterval()需要 2 个参数 - 要执行的函数,以及调用之间的延迟(以毫秒为单位)。它将继续调用该函数,直到您更改或翻页或调用clearInterval(intervalID). 您必须传入返回的值setInterval()才能清除它。
setTimeout()takes the same parameters, but it will only execute once. You can get around that by having the last line in the function you pass to setTimeout()call setTimeout()again.
setTimeout()采用相同的参数,但它只会执行一次。你可以通过在你传递的函数中的最后一行再次setTimeout()调用来解决setTimeout()这个问题。
I've noticed that in some browsers, you're restricted in what you can do with setInterval()- for instance, it might not let you call setInterval()on page load.
我注意到在某些浏览器中,您可以执行的操作受到限制setInterval()- 例如,它可能不允许您setInterval()在页面加载时调用。
回答by andres descalzo
I use this script Cookbook/waitwith a small plugin and it works pretty well:
我使用这个脚本Cookbook/wait和一个小插件,它工作得很好:
(function($){
$.fn.runItIf = function(ex, fn, args) {
var self = this;
return $(self).queue(function() {
args = args || [];
if (ex)
fn.apply.(self, args );
$(self).dequeue();
});
};
$.fn.runIt = function(fn, args) {
return $(this).runItIf(true, fn, args);
};
$.fn.wait = function(time, type) {
time = time || 1000;
type = type || "fx";
return this.queue(type, function() {
var self = this;
setTimeout(function() {
$(self).dequeue();
}, time);
});
};
})(jQuery);
Example based on the example in Cookbook/wait:
示例基于Cookbook/wait 中的示例:
function runIt() {
var expression = true;
$("div").wait()
.animate({left:'+=200'},2000)
.runIt(function() { /* ... */ } ).wait(10)
.runItIf(expression, function() { /* ... */ } ).wait(10)
.animate({left:'-=200'},1500,runIt);
}
runIt();

