setinterval

javascript ajax setinterval delay

匿名 (未验证) 提交于 2019-12-03 02:38:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: How can I execute this function immediately, then start the setInterval timer? Here is my js. $(document).ready(function(){ var callAjax = function(){ $.ajax({ method:'get', url:'apinvs.php', dataType:'html', success:function(data){ $("#main").html(data); } }); } setInterval(callAjax,2500); }); It's executing the function after 2.5 seconds. It leaves the div blank until 2.5 seconds pass, which is what I don't want to happen. :( I found a Stackoverflow question here but I'm not sure how to apply it with my code (it's using document ready).

what speed up “setInterval” when you switch page?

匿名 (未验证) 提交于 2019-12-03 02:29:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Here is My page,It is a demo to test moving a element,but after you change your page sometime,and back to here,why the DIV move faster? My css: #box1 { width: 900px; height: 50px; background-color: #000; position: relative; } #box2 { width: 50px; height: 50px; background-color: #a00; position: absolute; } My HTML: <div id="box1"> <div id="box2"></div> </div> My Js: var box2 = document.getElementById("box2"); var remove = setInterval(function () { box2.style.left = "0px"; var move = setInterval(function () { var newLeft = Math.min(parseInt

Execute the setInterval function without delay the first time

匿名 (未验证) 提交于 2019-12-03 02:05:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: It's there a way to configure the setInterval method of javascript to execute the method immediately and then executes with the timer 回答1: It's simplest to just call the function yourself directly the first time: foo (); setInterval ( foo , delay ); However there are good reasons to avoid setInterval - in particular in some circumstances a whole load of setInterval events can arrive immediately after each other without any delay. Another reason is that if you want to stop the loop you have to explicitly call clearInterval which

Does the browser keep track of active timer IDs?

这一生的挚爱 提交于 2019-12-03 01:46:52
Does the browser keep track of active setInterval and setTimeout IDs? Or is this solely up to the developer to keep track of? If it does keep track of them, is it accessible via the BOM? It is up for the developer to keep track of. You can do so by using the returned value of the setTimeout/setInterval function and passing that value to the clearTimeout/clearInterval function - as described in other answers here. This appears to be because each browser will implement keeping track of the intervals in their own way. From w3.org/TR/2009/WD-html5-20090212/no.html (a draft, but w3schools and http:

Can you use setInterval to call functions asynchronously?

匿名 (未验证) 提交于 2019-12-03 01:38:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: The following code is taken from Project Silk (a Microsoft sample application) The publish method below loops thru an an array of event callBacks and executes each one. Instead of using a for loop setInterval is used instead. The documentation says this allows each subscriber callback to be invoked before the previous callback finishes. Is this correct? I thought the browser would not allow the execution of the function inside the interval to run until all prior executions of it had finished. Is this really any different than doing a for

setInterval doesn&#039;t seem to like () in the function it invokes. Why?

匿名 (未验证) 提交于 2019-12-03 01:23:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: When I execute the following, incidentController gets called after 10 seconds and continues to execute with no problems every 10 seconds: // This works fine in nodejs v0.11.13 setInterval(incidentController, 10 * 1000); function incidentController () { console.log ('executed'); } However, this executes immediately and throws the following error on the second iteration: //This doesn't. The parens which wrap (123) cause the error. setInterval(incidentController(123), 10 * 1000); function incidentController (someInt) { console.log ('executed: '

前端基础之BOM和DOM

匿名 (未验证) 提交于 2019-12-03 00:40:02
ǰϷ 到目前为止,我们已经学过了JavaScript的一些简单的语法。但是这些简单的语法,并没有和浏览器有任何交互。 也就是我们还不能制作一些我们经常看到的网页的一些交互,我们需要继续学习BOM和DOM相关知识。 JavaScript分为 ECMAScript,DOM,BOM。 BOM(Browser Object Model)是指浏览器对象模型,它使 JavaScript 有能力与浏览器进行“对话”。 DOM (Document Object Model)是指文档对象模型,通过它,可以访问HTML文档的所有元素。 Window对象是客户端JavaScript最高层对象之一,由于window对象是其它大部分对象的共同祖先,在调用window对象的方法和属性时,可以省略window对象的引用。例如:window.document.write()可以简写成:document.write()。 window对象 所有浏览器都支持 window 对象。它表示浏览器窗口。 *如果文档包含框架(frame 或 iframe 标签),浏览器会为 HTML 文档创建一个 window 对象,并为每个框架创建一个额外的 window 对象。 *没有应用于 window 对象的公开标准,不过所有浏览器都支持该对象。 所有 JavaScript 全局对象、函数以及变量均自动成为 window 对象的成员

从js运行机制看setTimeout与setInterval

匿名 (未验证) 提交于 2019-12-03 00:26:01
setTimeout :超时调用; setInterval :间歇调用 调用时,代码是在全局环境中执行的,里面的 this 在正常模式下指向 window ,严格模式下指向 undefined 。 参数: 第一个参数为字符串或者函数(建议使用函数,传递字符串可能导致性能损失) 第二个参数为以毫秒表示的时间,该时间表示多少毫秒后将当前任务加入任务队列。 注意:对于定时器,是准时添加的,但不是准时执行的。 返回值: 调用setTimeout和setInterval后会返回一个数值ID,可以用来取消定时器 取消: clearTimeout(timeoutId) 、 clearInterval(intervalid) 注意:取消超时调用的重要性远远高于间歇调用。因为间歇调用会一直执行,直到页面卸载。 这两者都属于异步任务,由render进程下的定时器触发线程进行计数。 如: setTimeout( function () { console.log( "Hello,lwf" ); }, 1000 ); 当执行到如上代码时,首先上述代码除回调函数部分会进入任务栈,由js引擎线程(js内核)执行,发现是一个定时器(异步任务),由定时器触发线程开始计数。100ms后,将回调函数放入事件触发线程管理的任务队列中,继续等待,当执行栈和任务队列中前面代码都执行完毕后

为什么要用setTimeout模拟setInterval ?

匿名 (未验证) 提交于 2019-12-02 23:47:01
setInterval有两个缺点: 使用setInterval时,某些间隔会被跳过 ; 可能多个定时器会连续执行 ; 在前一个定时器执行完前,不会向队列插入新的定时器 (解决缺点一) 保证定时器间隔(解决缺点二) 例子: //使用setTimeout也可以产生setInterval的效果 var i=0; function intv(){ setTimeout(function(){ consolr.log(i++); intv() },1000) } intv()

vue中setInterval的清除

匿名 (未验证) 提交于 2019-12-02 23:41:02
两种清除setInterval的方式: 方案一: data() { return { timer: null // 定时器名称 } }, mouted() { this.timer = (() => { // 某些操作 }, 1000) }, beforeDestroy() { clearInterval(this.timer); this.timer = null; } 方案二( 官方推荐 ):通过$once事件侦听器器在定义完定时器之后的位置来清除定时器。 方案一有两个潜在的问题: timer ,如果可以的话最好只有生命周期钩子可以访问到它。这并不算严重的问题,但是它可以被视为杂物。 我们的建立代码独立于我们的清理代码,这使得我们比较难于程序化地清理我们建立的所有东西。 mounted() { const timer = setInterval(() =>{ // 某些定时器操作 }, 500); // 通过$once来监听定时器,在beforeDestroy钩子可以被清除。 this.$once('hook:beforeDestroy', () => { clearInterval(timer); }) }