JavaScript setInterval and `this` solution
this.intervalID = setInterval(this.retrieve_rate.bind(this), this.INTERVAL);
this.intervalID = setInterval(this.retrieve_rate.bind(this), this.INTERVAL);
You used a function call instead of a function reference as the first parameter of the setInterval. Do it like this: function timer() { console.log(“timer!”); } window.setInterval(timer, 1000); Or shorter (but when the function gets bigger also less readable): window.setInterval( function() { console.log(“timer!”); }, 1000)
I don’t think setInterval is inherently going to cause you significant performance problems. I suspect the reputation may come from an earlier era, when CPUs were less powerful. There are ways that you can improve the performance, however, and it’s probably wise to do them: Pass a function to setInterval, rather than a string. Have … Read more
Keep it simple. You can use a named function instead of an anonymous function; call it and set an interval for it. function doSomething() { console.log(“tick”); } doSomething(); setInterval(doSomething, 9000); Create a scope if necessary: (function() { function doSomething() { console.log(“tick”); } doSomething(); setInterval(doSomething, 9000); })(); Finally, the following works without creating or affecting x: … Read more
You could use a flag to keep track of the status: var output = $(‘h1’); var isPaused = false; var time = 0; var t = window.setInterval(function() { if(!isPaused) { time++; output.text(“Seconds: ” + time); } }, 1000); //with jquery $(‘.pause’).on(‘click’, function(e) { e.preventDefault(); isPaused = true; }); $(‘.play’).on(‘click’, function(e) { e.preventDefault(); isPaused = false; … Read more
As long as you have scope to the saved interval variable, you can cancel it from anywhere. In an “child” scope: var myInterval = setInterval(function(){ clearInterval(myInterval); },50); In a “sibling” scope: var myInterval = setInterval(function(){ foo(); },50); var foo = function () { clearInterval(myInterval); }; You could even pass the interval if it would go … Read more
Use clearInterval: var refreshId = setInterval(function() { var properID = CheckReload(); if (properID > 0) { clearInterval(refreshId); } }, 10000);
Why so complicated? When you can do: var loadTime = window.performance.timing.domContentLoadedEventEnd- window.performance.timing.navigationStart; If you need more times check out the window.performance object: console.log(window.performance); Will show you the timing object: connectEnd Time when server connection is finished. connectStart Time just before server connection begins. domComplete Time just before document readiness completes. domContentLoadedEventEnd Time after DOMContentLoaded event … Read more
This might be the correct snippet you were looking for: import threading def set_interval(func, sec): def func_wrapper(): set_interval(func, sec) func() t = threading.Timer(sec, func_wrapper) t.start() return t
You can create a setTimeout loop using recursion: function timeout() { setTimeout(function () { // Do Something Here // Then recall the parent function to // create a recursive loop. timeout(); }, 1000); } The problem with setInterval() and setTimeout() is that there is no guarantee your code will run in the specified time. By … Read more