问题描述
我想写一个循环,在每次迭代之后用间隔打印数字1到10
我该如何实现它?我尝试了睡眠()setInterval()setTimeout()和什么,但似乎无法找到任何工作的解决方案。如果可能,我想这样做只使用纯Javascript。
function abc(){
for(i = 1; i document.write(i +< br> ;);
sleep(1000);
}
}
<$ p (new Date()。)getTime()< = new Time()。getTime()+ dur;
while d){
//什么也不做
}
}
console.log(new Date()。getTime())
sleep(1000)
console.log(new Date()。getTime())
然后你可以在每次迭代之后调用 sleep
函数,比如
函数abc(){
for (i = 1; i document.write(i +< br>);
sleep(1000);
但是请注意 sleep
会在这段时间冻结你的浏览器,
你不会想这样的行为,当你只是想要周期性地做...
window.setInterval
在这种情况下就是你想要的
函数abcd(i){
document.write(i +< br>)
}
函数repeatTimeout(func,times,duration) {
var args = Array.prototype.slice.call(arguments).splice(3);
var i = 0;
args.push(i)
var wrap = function(){
if(args [args.length - 1]> = times)
window.clearInterval(wrap)
else {
func.apply(this,args)
args [args.length - 1] ++
}
}
window.setInterval(wrap,duration)
}
repeatedTimeout(abcd,10,1000)
每1000毫秒会调用10次,而不冻结Browers
继承人
更新
成为一个for循环,你可以做这样的事情,
,不管它对我有什么意义
for (函数(i){
return function(){
document.write(var i = 0; i window.setTimeout i +< br>)
}
})(i),i * 1000)
}
是另一种
在循环中调用 window.setTimeout
,并在 i
作为超时,
这将工作,但我宁愿建议使用 setInterval
像你已经在你发布的小提琴评论
I want to write a for loop which prints number 1 to 10 with intervals after every iteration "like this"
How can I achieve it? I tried sleep() setInterval() setTimeout(") and what not but can't seem to find any working solution. And if possible I would like to do this using pure Javascript only.
function abc(){
for(i=1;i<=10;i++){
document.write(i+"<br>");
sleep(1000);
}
}
解决方案 To answer the question, to get something like a sleep function you could just write somehting like this as a helper function
function sleep(dur) {
var d = new Date().getTime() + dur;
while(new Date().getTime() <= d ) {
//Do nothing
}
}
console.log(new Date().getTime())
sleep(1000)
console.log(new Date().getTime())
Then you could call the sleep
function after every iteration like
function abc(){
for(i=1;i<=10;i++){
document.write(i+"<br>");
sleep(1000);
}
}
But Note that sleep
will freeze your browser in this time andyou don't really wan't this kind of behaviour when you just want to periodiccally do sth
window.setInterval
would be what you want in such cases
function abcd(i){
document.write(i + "<br>")
}
function repeatedTimeout(func,times,duration) {
var args = Array.prototype.slice.call(arguments).splice(3);
var i = 0;
args.push(i)
var wrap = function () {
if(args[args.length - 1] >= times)
window.clearInterval(wrap)
else {
func.apply(this,args)
args[args.length - 1]++
}
}
window.setInterval(wrap,duration)
}
repeatedTimeout(abcd,10,1000)
Which would call it 10 times every 1000 milliseconds, whithout freezing the Browers
Heres the JSBin
Update
If it really has to be a for loop, you could do something like this,regardless of the sense it makes to me
for (var i = 0; i <= 10 ; i++) {
window.setTimeout(
(function (i){
return function() {
document.write(i + "<br>")
}
})(i),i * 1000)
}
In this case heres another JSBin
This would call window.setTimeout
in a for
loop and a multiple of the timeout with i
as the timeout, this would work, but i'd rather suggest using setInterval
like you already did in the Fiddle you posted in the comment
这篇关于为FOR LOOP中的每个迭代添加一个暂停/间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!