我希望我的for循环不应该立即执行,而是在每次迭代后等待超时.例如:
for(var i=0; i<10; i++) {
console.log(i);
//wait for 1000
}
Run Code Online (Sandbox Code Playgroud)
我在堆栈溢出中发现了许多像这样的解决方案:
for (var i=0;i<=10;i++) {
(function(ind) {
setTimeout(function(){console.log(ind);}, 3000);
})(i);
}
Run Code Online (Sandbox Code Playgroud)
但是在所有实现中,循环最初等待3000毫秒,然后立即执行整个for循环.有没有办法在等待1000毫秒后调用每个迭代.
这是我的问题.我有这个功能来测试代理服务器.
function crawl() {
var oldstatus = document.getElementById('status').innerHTML;
document.getElementById('status').innerHTML = oldstatus + "Crawler Started...<br />";
var url = document.getElementById('url').value;
var proxys = document.getElementById('proxys').value.replace(/\n/g,',');
var proxys = proxys.split(",");
for (proxy in proxys) {
var proxytimeout = proxy*10000;
setTimeout(doRequest(url,proxys[proxy]), proxytimeout);
}
}
Run Code Online (Sandbox Code Playgroud)
我希望以大约10秒的间隔调用'doRequest()'函数,但即使使用setTimeout(),也会立即调用函数.
欢迎任何想法,谢谢.
PS:即使我为'proxytimout'设置了一个任意值,它也没有效果.
我有一个表单,我试图提交页面加载后5秒已经过去了..我已经尝试了setTimeout但它似乎没有工作..任何人都可以建议为什么会这样,我有jQuery上网站,但无法得到延迟()工作
<form action="" name="cartCheckout" id="cartCheckout" method="post">
<input type="hidden" name="action" value="checkout" />
<input type="hidden" name="save" value="1" />
<input type="hidden" name="orderID" value="<?php echo $GLOBALS['CHECKOUT_ORDERID']; ?>" />
<div class="itembox" id="step4box">
<div id="progress">
<ul>
<li>Your Cart</li>
<li>Your Details</li>
<li class="current">Payment</li>
<li>Confirmation</li>
</ul>
</div>
<div id="words" class="gothbold">Please wait while we we redirect you to<br />Paypal for a secure payment method.</div>
<a class="redirect" href="javascript:void(0)" title="Proceed to Paypal" onClick="document.cartCheckout.submit();"><span>If you aren't redirected in 5 seconds click here</span></a><br />
<a class="cancel" href="javascript:void(0)" title="Return to details" onClick="jQuery('#step4box').hide();jQuery('#step2box').show();"><span>Cancel</span></a>
</div>
<script …Run Code Online (Sandbox Code Playgroud) 我想在我的Javascript代码中添加一个小骰子滚动效果.我认为一个好方法是使用该setInterval()方法.我的想法是遵循代码(仅用于测试):
function roleDice() {
var i = Math.floor((Math.random() * 25) + 5);
var j = i;
var test = setInterval(function(){
i--;
document.getElementById("dice").src = "./images/dice/dice" + Math.floor((Math.random() * 6) + 1) + ".png";
if(i < 1) {
clearInterval(test);
}
}, 50);
}
Run Code Online (Sandbox Code Playgroud)
现在我想等待setInterval,直到完成.所以我添加了一个setTimeout.
setTimeout(function(){alert("test")}, (j + 1) * 50);
Run Code Online (Sandbox Code Playgroud)
这段代码工作得很好.但在我的主代码中,该roleDice()函数返回一个值.现在我不知道我怎么能处理那个...我无法从那里回来setTimeout().如果我在函数末尾添加一个返回值,则返回将提升为快速.有没有人有想法,我怎么能解决这个问题?
编辑 嗯,好吧,我明白回调剂量是什么,我想我知道它是如何工作的,但我仍然有问题.我认为这更像是一个"界面"问题......我的代码:
function startAnimation(playername, callback) {
var i = Math.floor((Math.random() * 25) + 5);
var int = setInterval(function() {
i--;
var number = Math.floor((Math.random() * …Run Code Online (Sandbox Code Playgroud) 在Chrome中,如果用户滚动所有XHR并且setTimeouts将被延迟,直到滚动停止并且我需要一个解决方法.此博客文章中描述了此行为.虽然这个功能有助于移动滚动,但对于无限滚动来说却是灾难性的,这正是我想要做的.
发生这种情况的证据:
所有其他浏览器都能正常工作,Chrome会显示一个空白屏幕,直到用户停止滚动.
网络面板将显示所有请求,pending直到滚动结束,然后它们全部完成.
把它放在一个片段中,运行它然后立即开始滚动.滚动完成后才会调用setTimeout.
var p = new Promise(function (resolve) {
setTimeout(function () {
console.log('resolving');
resolve();
}, 1000)
});
p.then(function () {
console.log('DONE!!');
})
Run Code Online (Sandbox Code Playgroud) 我有个问题.我希望setTimeout在2秒后使用角度调用函数,但我得到了这个:
错误:找不到模块:错误:无法解析'计时器'; 这是我的功能:
login(user) {
console.log(user.value);
this.loginService.loginUser(user.value);
// this.user = this.loginService.getUser();
setTimeout(() => {
this.user = this.loginService.getUser();
}, 2000);
if (this.user === undefined) {
console.log('username or password incorrect');
} else {
console.log(this.user);
this.navbar.connectComps(this.user);
this.navbar.getCheck();
}
}
Run Code Online (Sandbox Code Playgroud)
请告诉我该怎么做才能解决这个问题.
好的,所以我有这个代码:
$(this).find('article.loading').each( function(i) {
var el = this;
setTimeout(function () {
$(el).replaceWith($('#dumpster article:first'));
}, speed);
});
Run Code Online (Sandbox Code Playgroud)
我想用另一个元素替换每个元素但我想在每个替换之间延迟.
我无法弄清楚为什么这不起作用,它只是在一次超时后替换所有这些.
有任何想法吗?
谢谢.
对于javascript爱好者,
你会如何编制一个setTimeOut(或setInterval)手柄来按分钟开火.因此,例如,如果是当前时间的51秒,则在9秒内触发,如果是第14秒,则在46秒内触发
谢谢
(我需要在浏览器上使用process.nextTick等效项.)
我正在努力充分利用javascript的性能,所以我做了一个简单的计数器...在一秒钟内我连续调用一个函数,只是将一个函数添加到变量中.
代码:codepen.io/rafaelcastrocouto/pen/gDFxt
我使用setTimeout大约250,使用google chrome/win7中的requestAnimationFrame获得70.我知道requestAnimationFrame与屏幕刷新率一致,所以我们怎样才能让它更快?
PS:我知道asm.js
我正在寻找一种在N秒后在nodejs中运行一些代码的方法.
尝试了setTimeout(),但它似乎完全阻止它,直到时间结束但这不是我想要的,因为我的服务器仍在发送和接收事件.
有什么建议?
settimeout ×10
javascript ×9
angular ×1
delay ×1
each ×1
for-loop ×1
forms ×1
jquery ×1
loops ×1
module ×1
node.js ×1
performance ×1
scroll ×1
setinterval ×1
timer ×1
wait ×1