setInterval() only running function once
Change: setInterval(getMessages(), queryInterval); To: setInterval(getMessages, queryInterval);
Change: setInterval(getMessages(), queryInterval); To: setInterval(getMessages, queryInterval);
Be carefull.. your first code would block JavaScript event loop. Basically in JS is something like list of functions which should be processed. When you call setTimeout, setInterval or process.nextTick you will add given function to this list and when the right times comes, it will be processed.. Your code in the first case would … Read more
EDIT: Yury’s answer is better. tl;dr IMO there is no memory leak. The positive slope is simply the effect of setInterval and setTimeout. The garbage is collected, as seen by sawtooth patterns, meaning by definition there is no memory leak. (I think). I’m not sure there is a way to work around this so-called “memory … Read more
You need to clear your interval, useEffect(() => { const intervalId = setInterval(() => { //assign interval to a variable to clear it. setState(state => ({ data: state.data, error: false, loading: true })) fetch(url) .then(data => data.json()) .then(obj => Object.keys(obj).map(key => { let newData = obj[key] newData.key = key return newData }) ) .then(newData => … Read more
A lot of other answers are focusing on a pattern that does work, but their explanations aren’t really very thorough as to why your current code doesn’t work. Your code, for reference: function funcName() { alert(“test”); } var func = funcName(); var run = setInterval(“func”,10000) Let’s break this up into chunks. Your function funcName is … Read more
Browser set a minimal value for the interval. Usualy 10ms, but it can depend on the browser. This means repeat this as fast as I’m possibly allowed. The W3C spec say 4ms : http://www.whatwg.org/specs/web-apps/current-work/multipage/timers.html#timers This is correct but probably reveal a design error. EDIT: By the way, it is bad practice to pass a string … Read more
var list = [1, 2, 3, 4, 5]; for (var i = 0, len = list.length; i < len; i += 1) { (function(i) { setInterval(function() { list[i] += 10; console.log(i + “=>” + list[i] + “\n”); }, 5000) })(i); } Here is the working code: var list = [1, 2, 3, 4, 5]; for … Read more
You have to store the timer id of the interval when you start it, you will use this value later to stop it, using the clearInterval function: $(function () { var timerId = 0; $(‘textarea’).focus(function () { timerId = setInterval(function () { // interval function body }, 1000); }); $(‘textarea’).blur(function () { clearInterval(timerId); }); });
I don’t think there is a way to enumerate active timers, but you could override window.setTimeout and window.clearTimeout and replace them with your own implementations which do some tracking and then call the originals. window.originalSetTimeout = window.setTimeout; window.originalClearTimeout = window.clearTimeout; window.activeTimers = 0; window.setTimeout = function(func, delay) { window.activeTimers++; return window.originalSetTimeout(func, delay); }; window.clearTimeout = … Read more
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