setTimeout returns a number, a timeoutId, which is used by clearTimeout to identify and remove it. However, what value do I use if I want to set the timeout conditionally? Is 0 a safe ID to use? It’s used in a React context where clearing the timeout is generally considered good practice. Or is it perhaps better to wrap the
Tag: settimeout
problem with settimeout function in slideshow speeding up after about 5 interations
The following code I have implemented for a javascript slideshow appears to speed up after about 5 iterations, from once every 15 seconds to what appears to be about 1 second between transitions. Is there something wrong with how the jquery/javascript implements the setTimeout? Thanks Answer Use setInterval instead. You are currently adding new setTimeouts for every loop I changed
How to print an array updated by setTimeout? [closed]
Closed. This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 7 months ago. Improve this question The task is to write a function to reverse an array of numbers and print out each number every 2 seconds.
How should I use the callback to get the desired outcome?
Hello there I am getting the desired result when am using promise but how should I implement it with callback when am returning something from the function. result with promise *** using callback *** OUTPUT should be I am first I am second I am third Answer You don’t need that global temp variable, and your setTimeout callback in second
Change of pictures over and over by using setInterval
Below is a program that is to loop pictures with fade-in effect. It includes this statement: I understand that this statement assigns the opacity value of a window object to a variable opacity, but there is no such opacity variable declaration of any elements in the program! When I remove this statement, it only shows the first picture… And when
JavaScript Why does an inner setTimeout of a Promise runs after an outside setTimeout
When run the below code snippet, it outputs 2,1. Since Promise is a micro-task and everything inside a promise should run before a macro-task (setTimeout), I expect that the output will be 1,2. So even if there is a macro-task inside a micro-task, I thought the output will be 1,2. But it outputs 2,1. What’s the catch here? Why does
Why I see unsafe-eval alert when using setTimeout with strings?
Here is the example how it appear in Chrome dev tools: String Refused to evaluate a string as JavaScript because ‘unsafe-eval’ is not an allowed source ofscript in the following Content Security Policy directive: “script-src’self’ ‘unsafe-inline’ https:”. What does this alert means and what is the security concern of using setTimeout with strings? Answer You’re using setTimeout wrong. When you
How do I wait for multiple promises to resolve but also include a set minumum delay?
I am calling two APIs with two different functions and having a setTimeout for both, is it possible to handle their timeouts in such a way that they finish in 10s instead of 15s. Is there a way to run this code only in 10s, so that both the APIs get called in 10s time period Answer Forcing either one
Does setTimeout() work differently at different hours?
I am currently trying to get a repeating sound effect, which is getting slower over time with setTimeout() in sync with an animation. As soon as I get it in sync it will work and stay in sync for the time I am working on the program. But now when I was away for about 1 1/2 hours and run
Difference between two ways of resolving a promise
I would like to understand the difference between the two code snippets, both resolving promises in different ways but receiving the same result (resolving after a second). Which one is the right way? Answer In this case, where you’re not resolving with a value, there is no difference. In situations where you need to resolve the promise with a value,