settimeout

Loop every five seconds in Javascript

一笑奈何 提交于 2020-07-18 07:18:52
问题 I'm trying to write a simple loop in JS (or JQuery) that updates an image every five seconds, for a total of 15 seconds (so three loops), and then quits. It should go like this: Wait five seconds Execute Wait five seconds Execute Wait five seconds Execute Quit But setTimeout only seems to work once. As a test, I've tried: function doSetTimeout(i) { setTimeout(function() { alert(i); }, 5000); } for (var i = 1; i <= 5; ++i) doSetTimeout(i); Does not work: http://jsfiddle.net/ubruksco/ I've also

Is it true that if possible I should never use setInterval & setTimeout?

天涯浪子 提交于 2020-07-17 08:18:20
问题 I am learning to code in JavaScript. I am programming something with some timed mouse animation. I'm just about to add some code which draws the mouse path. It's going to be something that takes the mousemove event, and every time the mouse moves draw a new line path on Canvas. And with time, that path is going to be more transparent until it disappears. Of course, new paths are always going to be opaque so there's a continuous movement. I figured out a way I can do this with just

Does Web Worker throttles setTimeout() / setInteval()?

拟墨画扇 提交于 2020-06-29 04:28:29
问题 I have a script on foreground tab that starts (dedicated) web worker. Now I see that setTimeout(xxx, 100) in that web worker is limited to be triggered not more often than once per second instead of 10 times per second as required. I've googled such a limitation for inactive tabs but are there any docs that say the same about Web Workers? I've checked this in Chrome and Firefox. 回答1: There is no real specs about that throttling behavior, even though they do allow it: Optionally, wait a

Does Web Worker throttles setTimeout() / setInteval()?

孤街醉人 提交于 2020-06-29 04:28:07
问题 I have a script on foreground tab that starts (dedicated) web worker. Now I see that setTimeout(xxx, 100) in that web worker is limited to be triggered not more often than once per second instead of 10 times per second as required. I've googled such a limitation for inactive tabs but are there any docs that say the same about Web Workers? I've checked this in Chrome and Firefox. 回答1: There is no real specs about that throttling behavior, even though they do allow it: Optionally, wait a

Debugging: ESLint Warning “Function declared in a loop contains unsafe references to variable(s)…no-loop-func”

空扰寡人 提交于 2020-06-28 04:49:27
问题 Building a Sort-Visualizer in React using the Create-React-App [https://roy-05.github.io/sort-visualizer/ ] I'm animating each iteration of the loop using setTimeouts. On dev console I get the following warning: Line 156:32: Function declared in a loop contains unsafe references to variable(s) 'minimum', 'minimum', 'minimum', 'minimum' no-loop-func Here's the code-snippet: for(let i=0; i<arr.length-1; i++){ let minimum = i; //Declare minimum here setTimeout(()=>{ for(let j = i+1; j<arr.length

Catching an exception from the setTimeout function executed inside an instance of a Promise object [duplicate]

独自空忆成欢 提交于 2020-06-21 05:37:07
问题 This question already has answers here : Asynchronous exception handling with bluebird promises (3 answers) Closed 5 days ago . Dear participants please tell me the solution. In this block of code, the catсh method perfectly catches the exception: const myPromise = new Promise(() => { throw new Error(`Oops! Threw an exception`); }); // We catch the exception in the method `catch`. myPromise .catch((error) => console.log(error.message)); And in this block, the catсh method will not be called:

Recursive function with timeout and check RxJs Observable value polling

牧云@^-^@ 提交于 2020-06-16 17:57:51
问题 I have the recursive function: repeatAlert that is called again if data.answered === null : .... Edit this.repeatAlert(id).subscribe( val => console.log(val)); console.log('1stCall Alert: ', new Date().getMinutes()); .... find(id: number): Observable<any> { return this.http.get(`${this.resourceUrl}ByAlertId/${id}` } repeatAlert(id: number) { this.find(id).subscribe((data: AlertInt) => { if (data.answered === null ) { this.sendNotification('Alert ', data.text); console.log('Call Alert: ', new

Recursive function with timeout and check RxJs Observable value polling

狂风中的少年 提交于 2020-06-16 17:57:50
问题 I have the recursive function: repeatAlert that is called again if data.answered === null : .... Edit this.repeatAlert(id).subscribe( val => console.log(val)); console.log('1stCall Alert: ', new Date().getMinutes()); .... find(id: number): Observable<any> { return this.http.get(`${this.resourceUrl}ByAlertId/${id}` } repeatAlert(id: number) { this.find(id).subscribe((data: AlertInt) => { if (data.answered === null ) { this.sendNotification('Alert ', data.text); console.log('Call Alert: ', new

keypress event doesn't log input value first time event is fired

老子叫甜甜 提交于 2020-06-14 15:04:57
问题 The first time a keypress event fires, it logs an empty input value even though the input has a value. The second time it logs the value but is one keystroke behind in comparison with the input's value. You can check this behavior on the next example: document.addEventListener('DOMContentLoaded', () => { const input = document.querySelector('input'); input.addEventListener('keypress', e => { console.log(e.target.value); }); }); <input type="text"/> However, the next workaround makes it work,

keypress event doesn't log input value first time event is fired

北慕城南 提交于 2020-06-14 15:02:51
问题 The first time a keypress event fires, it logs an empty input value even though the input has a value. The second time it logs the value but is one keystroke behind in comparison with the input's value. You can check this behavior on the next example: document.addEventListener('DOMContentLoaded', () => { const input = document.querySelector('input'); input.addEventListener('keypress', e => { console.log(e.target.value); }); }); <input type="text"/> However, the next workaround makes it work,