I'm trying to write a simple loop in JS (or JQuery) that updates an image every five seconds, for a total of 15 seconds (so three loops), and then quits.
我正在尝试在JS(或JQuery)中编写一个简单的循环,每隔五秒更新一次图像,总共15秒(所以三个循环),然后退出。
It should go like this:
它应该是这样的:
- Wait five seconds
- 等五秒钟
- Execute
- 执行
- Wait five seconds
- 等五秒钟
- Execute
- 执行
- Wait five seconds
- 等五秒钟
- Execute
- 执行
- Quit
- 放弃
But setTimeout
only seems to work once.
但是setTimeout似乎只工作一次。
As a test, I've tried:
作为测试,我尝试过:
function doSetTimeout(i) {
setTimeout(function() { alert(i); }, 5000);
}
for (var i = 1; i <= 5; ++i)
doSetTimeout(i);
Does not work: http://jsfiddle.net/ubruksco/
不起作用:http://jsfiddle.net/ubruksco/
I've also tried:
我也尝试过:
for(var i = 1; i <= 5; i++) {
(function(index) {
setTimeout(function() { alert(index); }, 5000);
})(i);
}
Does not work: http://jsfiddle.net/Ljr9fq88/
不起作用:http://jsfiddle.net/Ljr9fq88/
8 个解决方案
#1
10
var time = 1;
var interval = setInterval(function() {
if (time <= 3) {
alert(time);
time++;
}
else {
clearInterval(interval);
}
}, 5000);
you can simply create an interval and kill it after the 3rd time
你可以简单地创建一个间隔并在第3次之后将其杀死
#2
2
The reason is that your settimeout ends all at the same time (after 5 seconds) because your timeout code is based on 5 seconds
原因是你的settimeout在同一时间(5秒后)结束,因为你的超时代码是基于5秒
for(var i = 1; i <= 5; i++) {
(function(index) {
setTimeout(function() { alert(index); }, 5000);
})(i);
}
What you want to do is change the timeout time based on your index (hence will have different start times.
您要做的是根据您的索引更改超时时间(因此将有不同的开始时间。
for(var i = 0; i < 3; i++) {
(function(index) {
setTimeout(function() { alert(index); }, index*5000);
})(i);
}
(Also needs 3 iterations, so edited out the loop for you)
(还需要3次迭代,所以为你编辑了循环)
#3
1
Make it easy! You do not need loop, you just need three executions.
让它变得简单!你不需要循环,你只需要三次执行。
setTimeout(function() { alert(1); }, 5000);
setTimeout(function() { alert(2); }, 10000);
setTimeout(function() { alert(3); }, 15000);
But, if you really want a loop:
但是,如果你真的想要一个循环:
function doSetTimeout(i) {
setTimeout(function() { alert(i); }, i*5000);
}
for (var i = 1; i <= 3; ++i)
doSetTimeout(i);
#4
0
Assuming your are using jQuery (to manipulate the DOM),
假设您正在使用jQuery(操纵DOM),
you can try this:
你可以试试这个:
['img1.jpg', 'img2.jpg', 'img3.jpg'].forEach(function(imgPath, index) {
// the callback will be executed in 5seconds * (index + 1)
setTimeout(function() {
// change image source
$('img#myImage').attr('src', imgPath);
}, 5000 * (index + 1));
});
#5
0
With setTimeout:
使用setTimeout:
function doSetTimeout(i) {
if(i >= 3) return;
alert(i);
setTimeout((function () {
return function () {
doSetTimeout(i);
};
})(i + 1), 5000);
}
doSetTimeout(0);
But you can also use setInterval, maybe more appropriate.
但是你也可以使用setInterval,也许更合适。
#6
0
You can use setInterval
instead and track how much times you have executed function. Than just use clearInterval()
to stop execution.
您可以改用setInterval并跟踪执行函数的次数。而不仅仅是使用clearInterval()来停止执行。
var i = 1;
var interval = setInterval(function() {
execute();
}, 5000);
$(document).ready(function() {
execute();
});
function execute() {
$("#output").append("set<br/>");
if (i == 3) {
clearInterval(interval);
}
i++;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='output'></div>
If you want to first wait 5 secs, don't call execute()
on domready.
如果你想先等待5秒,不要在domready上调用execute()。
#7
0
Your first example is nearly there. You just need to multiply the time delay by the loop index to get the right delay.
你的第一个例子就在那里。您只需要将时间延迟乘以循环索引即可获得正确的延迟。
function doSetTimeout(i) {
setTimeout(function() { alert(i); }, 5000*i);
}
for (var i = 1; i <= 3; ++i)
doSetTimeout(i);
http://jsfiddle.net/ubruksco/3/
http://jsfiddle.net/ubruksco/3/
#8
-1
You want setInterval() instead
你想要setInterval()
setInterval(function(){ alert("Do Something"); }, 3000);
#1
10
var time = 1;
var interval = setInterval(function() {
if (time <= 3) {
alert(time);
time++;
}
else {
clearInterval(interval);
}
}, 5000);
you can simply create an interval and kill it after the 3rd time
你可以简单地创建一个间隔并在第3次之后将其杀死
#2
2
The reason is that your settimeout ends all at the same time (after 5 seconds) because your timeout code is based on 5 seconds
原因是你的settimeout在同一时间(5秒后)结束,因为你的超时代码是基于5秒
for(var i = 1; i <= 5; i++) {
(function(index) {
setTimeout(function() { alert(index); }, 5000);
})(i);
}
What you want to do is change the timeout time based on your index (hence will have different start times.
您要做的是根据您的索引更改超时时间(因此将有不同的开始时间。
for(var i = 0; i < 3; i++) {
(function(index) {
setTimeout(function() { alert(index); }, index*5000);
})(i);
}
(Also needs 3 iterations, so edited out the loop for you)
(还需要3次迭代,所以为你编辑了循环)
#3
1
Make it easy! You do not need loop, you just need three executions.
让它变得简单!你不需要循环,你只需要三次执行。
setTimeout(function() { alert(1); }, 5000);
setTimeout(function() { alert(2); }, 10000);
setTimeout(function() { alert(3); }, 15000);
But, if you really want a loop:
但是,如果你真的想要一个循环:
function doSetTimeout(i) {
setTimeout(function() { alert(i); }, i*5000);
}
for (var i = 1; i <= 3; ++i)
doSetTimeout(i);
#4
0
Assuming your are using jQuery (to manipulate the DOM),
假设您正在使用jQuery(操纵DOM),
you can try this:
你可以试试这个:
['img1.jpg', 'img2.jpg', 'img3.jpg'].forEach(function(imgPath, index) {
// the callback will be executed in 5seconds * (index + 1)
setTimeout(function() {
// change image source
$('img#myImage').attr('src', imgPath);
}, 5000 * (index + 1));
});
#5
0
With setTimeout:
使用setTimeout:
function doSetTimeout(i) {
if(i >= 3) return;
alert(i);
setTimeout((function () {
return function () {
doSetTimeout(i);
};
})(i + 1), 5000);
}
doSetTimeout(0);
But you can also use setInterval, maybe more appropriate.
但是你也可以使用setInterval,也许更合适。
#6
0
You can use setInterval
instead and track how much times you have executed function. Than just use clearInterval()
to stop execution.
您可以改用setInterval并跟踪执行函数的次数。而不仅仅是使用clearInterval()来停止执行。
var i = 1;
var interval = setInterval(function() {
execute();
}, 5000);
$(document).ready(function() {
execute();
});
function execute() {
$("#output").append("set<br/>");
if (i == 3) {
clearInterval(interval);
}
i++;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='output'></div>
If you want to first wait 5 secs, don't call execute()
on domready.
如果你想先等待5秒,不要在domready上调用execute()。
#7
0
Your first example is nearly there. You just need to multiply the time delay by the loop index to get the right delay.
你的第一个例子就在那里。您只需要将时间延迟乘以循环索引即可获得正确的延迟。
function doSetTimeout(i) {
setTimeout(function() { alert(i); }, 5000*i);
}
for (var i = 1; i <= 3; ++i)
doSetTimeout(i);
http://jsfiddle.net/ubruksco/3/
http://jsfiddle.net/ubruksco/3/
#8
-1
You want setInterval() instead
你想要setInterval()
setInterval(function(){ alert("Do Something"); }, 3000);