草庐IT

setTimeout

全部标签

javascript - 在 JavaScript 中使用 setTimeout() 和 setInterval() 时调用函数

这个问题在这里已经有了答案:Whatisthedifferencebetweenafunctioncallandfunctionreference?(6个答案)关闭1年前。如果我使用不带括号的setTimeout()和setInterval()调用命名函数,它会按预期工作。当我用括号调用同一个函数时,它要么立即执行要么给出错误。与我在网上找到的内容相比,我正在寻找对此事更深入的了解。你们能给我解释一下为什么这是真的吗?varfunc=function(){console.log("Bowtiesarecool.");}setTimeout(func(),1500);//Prints"B

javascript - 使用核心javascript时ajax调用中的settimeout

我有一个调用ajax的JavaScript函数。现在我需要在此函数中添加超时,就像调用服务花费的时间超过污染时间时,ajax调用应该超时并显示默认消息。我不想在其中使用Jquery。这是我的代码:AJAX=function(url,callback,params){vardt=newDate();url=(url.indexOf('?')==-1)?url+'?_'+dt.getTime():url+'&_'+dt.getTime();if(url.indexOf('callback=')==-1){ajaxCallBack(url,function(){if(this.readySt

javascript - 为什么 setInterval() 周期每次都变快?

我正在Javascript上构建自定义slider,我希望每次用户单击slider的div时,slider都应停止X秒。我的代码是:$(document).ready(function(){varciclo;varindex_slide=1;functionstartSlidercicle(){ciclo=setInterval(function(){//Slidercodegoeshere},3000);}//HereIstarttheslideranimationstartSlidercicle();//Whentheuserclicksonadivcalled'slide',st

javascript - Javascript 事件队列是否是一个简单的 FIFO?

看看这个例子:functionA(){console.log('A');}functionB(){console.log('B');}//andthenisetTimeout(fn,0)bothofthemsetTimeout(A,0);setTimeout(B,0);是否保证B会在A之后立即运行?浏览器是否可能在A和B之间的队列中添加另一个任务?注意:A或B函数都没有向事件循环添加任何新任务。varcallbacks=[];//thenaddabunchofcallbacks...(noneaddseventstoeventqueue)//case1:callbacks.forEac

javascript - 为什么 setTimeout(.., 0) 不立即执行?

vartimeout=setTimeout(function(){console.log("I'mmessagefromtimeout");},0);console.log("I'mmessagefromoutsidetimeout");//1.I'mmessagefromoutsidetimeout//2.I'mmessagefromtimeout为什么内部指令不首先执行,尽管将setTimeout时间设置为0?我使用了各种时间,包括0/null,我想知道如何保留setTimeout对象并随流程执行其指令。 最佳答案 Javasc

javascript - 禁用 Jest setTimeout 模拟

我正在为依赖于websocket库的代码编写Jest测试。websocket库被模拟。我想发送一条消息,等待异步操作完成,然后检查响应。it('sendsamessageandgetsaresponse',()=>{processor(ws).sendMessage()//doabunchofasyncstuff,callwebsocket.sendMessage()setTimeout(()=>{expect(ws.getResponse()).toEqual('alldone')},100)})不幸的是,因为Jest模拟了setTimeout,所以setTimeout失败了。如果我

javascript - 我不完全理解 JavaScript 线程

在我深入探讨这个问题之前。让我声明,通过事件循环,我指的是http://en.wikipedia.org/wiki/Event_loop.这是浏览器实现的东西。有关更多信息,请阅读:http://javascript.info/tutorial/further-javascript-features/events-and-timing-depth.这个问题又难又长,所以,请耐心等待!我非常感谢所有的回答!所以。现在,据我了解,在JavaScript中只有一个主线程(在大多数浏览器环境中)。所以,代码如下:for(varcolor=0x000;color会产生一个从黑到白的动画,但是你看

javascript - SetTimeout 后获取返回值

我刚才问了关于按名称调用函数的问题,现在我想在SetTimeout之后处理return语句:functionECall(funcName,arg){command+="(";for(vari=1;isetTimeout效果很好,但我必须保存被调用函数的返回值。当我写:setTimeout('alert(window[\''+funcName+'\']'+command+')',1000);它提醒函数的返回值。我该如何存储它? 最佳答案 您不需要使用任何这种字符串操作。只需将函数引用传递给window.setTimeout()。要存储

javascript - XHR 请求的 setTimeout

引用MDC:Ifthereisapossibilitythatyourlogiccouldtakelongertoexecutethantheintervaltime,itisrecommendedthatyourecursivelycallanamedfunctionusingwindow.setTimeout.Forexample,ifusingsetIntervaltopollaremoteserverevery5seconds,networklatency,anunresponsiveserver,andahostofotherissuescouldpreventtherequ

javascript - javascript 中的非阻塞 setTimeout 与 ruby​​ 中的 sleep

因此,在javascript中,由于其本质上是事件驱动的,因此setTimeout似乎不会阻塞。这意味着如果我这样做:setTimeout(function(){console.log('sleeping');},10);console.log('printsfirst!!');它将输出'printsfirst!!'然后是'sleeping'。js解释器不会等到setTimeout完成,而是立即执行下面的代码。10ms后,执行回调函数。现在我最近一直在玩ruby。我知道它在事件机器库中有非阻塞支持。但我想知道我们是否可以在没有事件机器支持的情况下实现类似于我刚刚用javascript使