Home > front end > How to ensure the project in one of the setinterval () than other setouttime () to perform first.
How to ensure the project in one of the setinterval () than other setouttime () to perform first.
Time:12-13
A function, it is put in setouttime (), performed setouttime (fn (), 0), and now need a key parameters, was obtained from the other manufacturers to obtain the key method is an asynchronous delays, setinterval (getKey (), 50), How to guarantee to get the key, and then executed fn (), First will not fn () into the getKey (), bosses solutions,
CodePudding user response:
I know, setoutime () and seiinterval () are asynchronous macros, their execution order not sure, may be the first execution, likely the first execution,
CodePudding user response:
Said demand, not necessarily to use both of it
CodePudding user response:
"Setoutime () and seiinterval () are asynchronous macro" this is not correct, both just timing to perform a piece of code,
Getkey () to do something, if you can't modify it can monitor it change
CodePudding user response:
Timeout call: setTimeout (expression, delay time) after the completion of a page load delay after a specified time to perform a expression Intermittent call: setInterval (expression, ms) after the page load to complete every millisecond performs a
Again, do you want to consider using Promise object to try
CodePudding user response:
Setouttime () also to setinterval bai, judge did getkey implementation, did not return to perform the execution of fn then remove the timer; The two asynchronous also not to interfere in each other, only such polling
CodePudding user response:
Why not promise? Even when using the timer does not guarantee can get data, network is not good will not return the data within the prescribed time, take a alright with this 50 milliseconds, general HTTP request is difficult to in 50 ms response data
Getkey response fn, later don't need a timer
Getkey always changing? Why do you want to write setinterval for many times?
CodePudding user response:
If the network time delay, you will be the manufacturer of the API make its, delay a 10 seconds will pile up hundreds of requests at once,
CodePudding user response:
With timer, is the key in transformation, if it is an asynchronous, can use as the upstairs said promise also can directly use the new async and await waiting to get the key to implement settimeout function if it is a request for many times to perform again, you can declare a variable, when to get the key to change the state of the variables, perform settiemeout by judging the state of the variables