recursive function vs setInterval vs setTimeout javascript

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

How to setInterval for every 5 second render with React hook useEffect in React Native app?

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

Javascript setInterval not working

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

setInterval() behaviour with 0 milliseconds in JavaScript

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

How to stop “setInterval” [duplicate]

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); }); });

Viewing all the timeouts/intervals in javascript?

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

Calculating Page Load Time In JavaScript

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