DWQA QuestionsCategory: ProgramConfusion about setinterval and JS single thread
Liuzi asked 53 mins ago

My personal understandingJavaScript is a single thread. Using setinterval is to add things to this single thread regularly. Then I use two setintervals to request two different data interfaces, so there will be no concurrency. I request data in queue order. So why is there a jam when using setinterval to request data interfaces,What, carton? The network is pending? Or page Caton?
How do you write setinterval? What effect do you want to achieve?
It’s best to post the corresponding code for analysis,JS multithreading requires the cooperation of web workers. How many setintervals are written will not cause multithreading. All tasks will be queued in turn. Page jams are simply cyclic calls that are too frequent or the interface is too slow.,JSThe way to deal with asynchronous tasks is similar to that when you wait for the rice to be stewed in an electric rice cooker, you can fry instead of waiting for the rice to be stewed.
But your ability is limited after all. If you are allowed to stew dozens of pots of rice, the rice here has not been washed, and the rice there has been burnt. At this time, you will be “stuck”.
setInterval + XHRHTTPRequestIt’s like asking you to stew another pot of rice every few minutes. The number of concurrent requests on the same page and domain name is limited. Chrome seems to have six requests, which means you only have six rice cookers, but the task of cooking has been increasing.

joyqi replied 16 hours ago

I’ll delete it if I don’t add any more

joyqi replied 16 hours ago

Please use the comment function

joyqi replied 16 hours ago

Questions should be put in comments, not in answers.

joyqi replied 16 hours ago

@fenYes, it is executed at the terminal

joyqi replied 16 hours ago

Please ignore this article

3 Answers
liuhy answered 16 hours ago

What, carton? The network is pending? Or page Caton?
How do you write setinterval? What effect do you want to achieve?
It’s best to post the corresponding code for analysis

liubingyang answered 16 hours ago

JS multithreading requires the cooperation of web workers. How many setintervals are written will not cause multithreading. All tasks will be queued in turn. Page jams are simply cyclic calls that are too frequent or the interface is too slow.

You trace my heart answered 16 hours ago

JSThe way to deal with asynchronous tasks is similar to that when you wait for the rice to be stewed in an electric rice cooker, you can fry instead of waiting for the rice to be stewed.
But your ability is limited after all. If you are allowed to stew dozens of pots of rice, the rice here has not been washed, and the rice there has been burnt. At this time, you will be “stuck”.
setInterval + XHRHTTPRequestIt’s like asking you to stew another pot of rice every few minutes. The number of concurrent requests on the same page and domain name is limited. Chrome seems to have six requests, which means you only have six rice cookers, but the task of cooking has been increasing.