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

CodePudding user response:

reference 2 floor console. The log () response:
said demand, not necessarily to use the two

Demand, our company bought a token key, manufacturer provides a set of js code for the key, and the code is to use the setinterval () time to get the key,

CodePudding user response:

wind 灬 cloud reference 5 floor response:
the setouttime () to setinterval bai, judge did getkey implementation, did not return to perform the execution of fn then remove the timer; Two asynchronous will not interfere with each other, only this polling

The question is how to perform first getKey () to invoke fn (), geyKey is manufacturer to provide method, fn is the company's underlying encapsulation method,

CodePudding user response:

The
refer to 6th floor jio's reply:
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?

That is to say, the key is the manufacturer provides a u disk, and then he gave me a set of access in a u disk serial number of js method, and then I found this method was obtained with setinterval interval, success after a cleansetinterval (), I have no language,

CodePudding user response:

references to the tenth floor to send reply:
Quote: refer to fifth floor wind 灬 cloud response:
the setouttime () to setinterval bai, judge did getkey implementation, did not return to perform the execution of fn then remove the timer; Two asynchronous will not interfere with each other, only this polling

The question is how to perform first getKey () to invoke fn (), geyKey is manufacturer to provide method, fn is the company's underlying encapsulation method,


Are you fn not write their own logic getKey () the results of the always can you judge? People use setinterval you also use the setinterval judgment doesn't work?

CodePudding user response:

refer to 12th floor wind 灬 cloud response:
Quote: reference to the tenth floor to send reply:

Quote: 5 reference building wind 灬 cloud response:
the setouttime () to setinterval bai, judge did getkey implementation, did not return to perform the execution of fn then remove the timer; Two asynchronous will not interfere with each other, only this polling

The question is how to perform first getKey () to invoke fn (), geyKey is manufacturer to provide method, fn is the company's underlying encapsulation method,


Are you fn not write their own logic getKey () the results of the always can you judge? People use setinterval you also use the setinterval judgment doesn't work?

This is a train of thought

CodePudding user response:

references to 11 floor, reply:
that is to say, the key is the manufacturer provides a u disk, and then he gave me a set of access in a u disk serial number of js method, and then I found this method was obtained with setinterval interval, success after a cleansetinterval (), I have no language,

To hear you say so, you mean you can't judge getKey execution results ah, you finally how to get a key value?? Got to fn ()

CodePudding user response:

You want to define a global variable, I will change after getKey, fn () in the judgment changed to continue to implement global variables

CodePudding user response:

This is very simple, use the Promise, can make two separate logic code and beautiful, you can direct messages to me, I teach you

CodePudding user response:

Fecth (" url "). Then (res=& gt; {
//res is access to the content, through the text (), json () deal with access to key
//here you can understand for your getKey ()
The return key
}). Then (key=& gt; {
//here is your fn ()
})

CodePudding user response:

Fecth (" url "). Then (res=& gt; {
//res is access to the content, through the text (), json () deal with access to key
//here you can understand for your getKey ()
Return res. The text ()//the above there is a mistake
}). Then (key=& gt; {
//here is your fn ()
})

CodePudding user response:

Why not use fn () into the getKey ()?? This is the most simple callback

That no words can also see promise

Modify the code logic can obtain from the business level
  • Related