草庐IT

SettimeOut

全部标签

javascript - 在设定时间之前结束 setTimeout 函数

我有一个jquery函数,当点击它时会产生一个设置的超时时间,使div可见。但是,如果在settimeout长度内选择了另一个选项,我想知道如何破坏这个函数并阻止它发生的任何其他事情。我当前的代码是:$(document).ready(function(){$('li#contact').click(function(){$('ul.image_display').css('display','none');$('ul.projects').fadeOut().hide();$('li#cv').removeClass('cur');$('li#projects').removeCla

javascript - 1 秒后调用 JavaScript 函数一次

我已经成功地使用setInterval函数在400毫秒后点击时隐藏了一个div。我的问题是它持续运行,我只需要函数执行一次。快速搜索后,我发现setInterval可以被clearInterval停止。我用错了吗?单击时将执行closeAnimation函数。我根据此页面上的代码为我的代码建模:http://www.w3schools.com/jsref/met_win_setinterval.aspfunctioncloseAnimation(){setInterval(function(){hide()},400);clearInterval(stopAnimation);}var

javascript - Firefox onLocationChange 并不总是被调用

我正在构建一个firefox扩展,它创建了几个隐藏的浏览器元素。我想addProgressListener()来处理我加载的页面的onLocationChange。但是,我的处理程序并不总是被调用。更具体地说,这是我正在做的:创建一个浏览器元素,不设置其src属性将它附加到另一个元素为浏览器元素添加一个监听onLocationChange的进度监听器使用所需的url和post数据调用loadURIWithFlags()我希望每次在4之后调用处理程序,但有时它不会(尽管它似乎卡在相同的页面上)。有趣的是,如果我将3和4包装在setTimeout(...,5000);中,它每次都有效。我也

javascript - setTimeout/clearTimeout 贵吗?

我有如下内容:varmyTimeout;functiondelayStuffUntil(when){if(myTimeout)clearTimeout(myTimeout);myTimeout=setTimeout(stuff,when-Date.now());}delayStuffUntil会被调用很多次,很可能会连续多次使用相同的when值调用它。setTimeout/clearTimeout是否足够昂贵以至于我应该检查当前的when与我最后一个when的值>(并且只有在不同时才更改计时器)?我考虑过这样做,但事实是when比较起来有点麻烦,而且我认为过早的优化是万恶之源,所以我可

javascript - 监视 Karma 和 Jasmine 中的 setTimeout 和 clearTimeout

我似乎无法在通过Karma运行的Jasmine测试中监视setTimeout和clearTimeout。我已经尝试了所有这些的变体spyOn(window,'setTimeout').and.callFake(()=>{});spyOn(global,'setTimeout').and.callFake(()=>{});spyOn(window,'clearTimeout').and.callThrough();clock=jasmine.clock();clock.install();spyOn(clock,'setTimeout').and.callThrough();runMyC

带有 setTimeout() 的 Javascript 执行顺序

假设我有以下代码:functiontestA{setTimeout('testB()',1000);doLong();}functiontestB{doSomething();}functiondoLong(){//takesafewsecondstodosomething}我执行testA()。我读过Javascript是单线程的。1000毫秒后,当testB()达到超时时会发生什么?我能想到的一些可能性:testB()排队等待在doLong()和它调用的任何其他内容完成后执行。doLong()立即终止并启动testB()。doLong()在停止(自动或在提示用户后)之前执行一段时间

javascript - setTimeout 和 V8

我已经独立安装了V8并执行如下javascript代码:./d8source.js。当我使用setTimeout时,我收到ReferenceError:setTimeoutisnotdefined。这是应该的吗?是否可以以某种方式包含此功能? 最佳答案 setTimeout不是ECMA-262的一部分,它是由浏览器实现的。但是,如果您安装Node.js(这是V8+extras)你会得到一个命令行setTimeout。 关于javascript-setTimeout和V8,我们在Stack

javascript - Promise.resolve 没有传入任何参数

在OpenUI5code-base我看到了这个片段://Waituntileverythingisrendered(parentheight!)beforereading/updatingsizes.//Useapromisetomakesure//tobeexecutedbeforetimeoutsmaybeexecuted.Promise.resolve().then(this._updateTableSizes.bind(this,true));它看起来像nativePromisefunction正在使用,没有参数传递给它的resolve需要一个函数:Argumenttobere

javascript - 检查Javascript中是否存在事件超时

有没有办法查明是否有事件的定时器?我有n个持续时间不同的计时器,例如:Timer1->2-secTimer2->8-sec.....Timern->n-sec我需要知道所有计时器何时结束HTMLTimeout1:Timeout2:Arealltimersfinished?JSsetTimeout(function(){$("#time-out-1span").text("Finished!");},2000);setTimeout(function(){$("#time-out-2span").text("Finished!");},8000);$('button').click(fu

javascript - 使用 setTimeout 的自定义对象调用方法丢失范围

我在构建Javascript对象和使用setTimeout调用该对象内的方法时遇到问题。我尝试了各种解决方法,但总是在循环的第二部分,范围变成窗口对象而不是我的自定义对象。警告:我对javascript还很陌生。我的代码:$(function(){slide1=Object.create(slideItem);slide1.setDivs($('#SpotSlideArea'));slide1.loc='getSpot';slide2=Object.create(slideItem);slide2.setDivs($('#ProgSlideArea'));slide2.loc='get