setinterval

setTimeout和setInterval

邮差的信 提交于 2019-11-29 01:57:45
1.setTimeout 1)第一个参数 // 第一个参数不仅可以是function, 还可以是一段可执行代码的字符串格式 setTimeout('console.log(1)', 1000); // 一秒后打印1 // 如果上面的代码没有字符串包含,则会立即打印,定时器不起效果 setTimeout(function timer() { console.log(1); },1000); // 效果和上面的字符串功能相同 2) 第二个参数 setTimeout(fn) 等同于 setTimeout(fn, 0); // 第二个参数没有的时候默认是0,但其实是等于4ms 3)第三个及之后的参数 setTimeout(function timer(a,b) { console.log(a,b); // 1,2 }, 1000, 1,2); // 当有第三个及之后的参数时,作为回调函数的参数传入 4) 返回值 // 返回一个数字,表示定时器编号,数字以此加1 const a = setTimeout(fn, 1000); // a =4 const b = setTimeout(fn, 1000); // b = 5 const c = setTimeout(fn, 1000); // c = 6 5) 关于this // this可以理解成调用该函数的对象 // 1) 直接写入回调函数

how do I pause and resume a timer?

烈酒焚心 提交于 2019-11-29 01:10:48
问题 I got this function that starts a timer on this format 00:00:00 whenever I click on a button. But I don't know how to do functions resume and pause. I've found some snippets that I thought could be helpful but I couldn't make those work. I'm new to using objects in js. function clock() { var pauseObj = new Object(); var totalSeconds = 0; var delay = setInterval(setTime, 1000); function setTime() { var ctr; $(".icon-play").each(function () { if ($(this).parent().hasClass('hide')) ctr = ($(this

Node.js crashes when using long interval in setinterval

眉间皱痕 提交于 2019-11-29 01:02:58
function createSasTokenTimer() { console.log("Hello"); } setInterval(createSasTokenTimer, 3000000); I run this code and after 50 minutes I get the following error: Hello timers.js:265 callback.apply(this, args); ^ TypeError: Cannot read property 'apply' of undefined at wrapper [as _onTimeout] (timers.js:265:13) at Timer.listOnTimeout (timers.js:110:15) When the interval time is shorter ( 2000000 for example), everything works fine. Is this a bug in Node.js? Update: OS: Windows , Node.js version: 0.12.4 When I run only the code above it works fine, but it does break when it's inside my

Using setInterval() to do simplistic continuous polling

微笑、不失礼 提交于 2019-11-28 22:35:20
For a simple webapp that needs to refresh parts of data presented to the user in set intervals, are there any downsides to just using setInterval() to get a JSON from an endpoint instead of using a proper polling framework? For the sake of the example, lets say I'm refreshing status of a processing job every 5 seconds. From my comment: I would use setTimeout [docs] and always call it when the previous response was received. This way you avoid possible congestion or function stacking or whatever you want to call it, in case a request/response takes longer than your interval. So something like

How to start and stop/pause setInterval?

不想你离开。 提交于 2019-11-28 18:19:41
I'm trying to pause and then play a setInterval loop. After I have stopped the loop, the "start" button in my attempt doesn't seem to work : input = document.getElementById("input"); function start() { add = setInterval("input.value++", 1000); } start(); <input type="number" id="input" /> <input type="button" onclick="clearInterval(add)" value="stop" /> <input type="button" onclick="start()" value="start" /> Is there a working way to do this? Matt Ball The reason you're seeing this specific problem: JSFiddle wraps your code in a function, so start() is not defined in the global scope . Moral

Javascript setInterval function to clear itself?

假如想象 提交于 2019-11-28 18:18:59
问题 myInterval = setInterval(function(){ MyFunction(); },50); function MyFunction() { //Can I call clearInterval(myInterval); in here? } The interval's not stopping (not being cleared), if what I've coded above is fine then it'll help me look elsewhere for what's causing the problem. Thanks. EDIT: Let's assume it completes a few intervals before clearInterval is called which removes the need for setTimeout. 回答1: As long as you have scope to the saved interval variable, you can cancel it from

What is the equivalent of javascript setTimeout in Java?

北城以北 提交于 2019-11-28 17:51:51
I need to implement a function to run after 60 seconds of clicking a button. Please help, I used the Timer class, but I think that that is not the best way. DavidPostill "I used the Timer class, but I think that that is not the best way." The other answers assume you are not using Swing for your user interface (button). If you are using Swing then do not use Thread.sleep() as it will freeze your Swing application. Instead you should use a javax.swing.Timer . See the Java tutorial How to Use Swing Timers and Lesson: Concurrency in Swing for more information and examples. Asynchronous

How to exit from setInterval

南楼画角 提交于 2019-11-28 17:44:55
I need to exit from a running interval if the conditions are correct: var refreshId = setInterval(function() { var properID = CheckReload(); if (properID > 0) { <--- exit from the loop---> } }, 10000); Use clearInterval : var refreshId = setInterval(function() { var properID = CheckReload(); if (properID > 0) { clearInterval(refreshId); } }, 10000); Updated for ES6 You can scope the variable to avoid polluting the namespace: const CheckReload = (() => { let counter = - 5; return () => { counter++; return counter; }; })(); { const refreshId = setInterval( () => { const properID = CheckReload();

Angular2/4 : Refresh Data Realtime

孤者浪人 提交于 2019-11-28 17:19:07
I need to refresh the data in a component page in an interval. Also I need to refresh the data after doing some action. I am using Obeservables in the service so that I can subscribe to when the response is ready. I am pushing the subscriptions to a object so that I can clear that on ngDestroy , I think, I have the following methods to achieve the same. Method 1 : setInterval I have set an interval on ngOnInit , which will call the refreshData in equal interval. The interval object will be cleared using clearInterval in ngOnDestroy method. export class MyComponent implements OnInit, OnDestroy

Can clearInterval() be called inside setInterval()?

江枫思渺然 提交于 2019-11-28 17:16:03
问题 bigloop=setInterval(function () { var checked = $('#status_table tr [id^="monitor_"]:checked'); if (checked.index()===-1 ||checked.length===0 || ){ bigloop=clearInterval(bigloop); $('#monitor').button('enable'); }else{ (function loop(i) { //monitor element at index i monitoring($(checked[i]).parents('tr')); //delay of 3 seconds setTimeout(function () { //when incremented i is less than the number of rows, call loop for next index if (++i < checked.length) loop(i); }, 3000); }(0)); //start