Understanding of pollingIn fact, the focus of polling is on the interval between executions, not the loop itself. Ajax is an asynchronous request, from initiating the request to receiving the response is a complete process. The time required for this process is unpredictable. To put it in an extreme point, if the time required for the request exceeds our polling interval, many problems will arise. Therefore, the polling interval should be based on ensuring that the request process is completed, which is more logical. Business Description:
Business logic point analysis:
Implementation ideas
// Polling method polling (page) { this.getWorks(page).then(res => { this.pollingST = setTimeout(() => { clearTimeout(this.pollingST) this.polling(page) }, 10000) }) } Why not use setInterval The function of setInterval seems to be a perfect match for the concept of polling. If our operation is synchronous code, then there is no problem in using setInterval. The problem is that setInterval is not flexible enough and we cannot know whether the last request has been completed. So setTimeout would be better. Things to note In the polling, I recorded the timer with the pollingST variable. The previous timer must be cleared before each execution. Because the recursive call is inside the timer, it is very convenient to end the polling by clearing the timer. Complete pseudo code<script> export default { data () { return { pollingST: null } }, methods: { // Paging change event pageChange (params) { // Clear the existing timer clearTimeout(this.pollingST) //Call polling this.polling(params) }, // Request interface method getWorks() { return new Promise(resolve => resolve({})) }, // Polling method polling (params) { this.getWorks(params).then(res => { this.pollingST = setTimeout(() => { clearTimeout(this.pollingST) this.polling(params) }, 10000) }) } }, created () { // Call polling this.polling({ page: 1, pageSize: 5 }) }, destroyed () { clearTimeout(this.pollingST) } } </script> SummarizeThis is the end of this article about the vue polling solution. For more relevant vue polling solutions, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Alibaba Cloud Ubuntu 16.04 builds IPSec service
>>: Example of how to implement MySQL cascading replication
Recently, an error occurred while starting MySQL....
Table of contents Features Preservation strategy ...
Table of contents 1. Introduction to jQuery 2. jQ...
Implementation ideas The outermost is a big circl...
In this experiment, we configure MySQL standard a...
HTML structure <body> <div class="w...
Now most of the Docker images are based on Debian...
Table of contents Vue first screen performance op...
The replace statement is generally similar to ins...
Table of contents 1. What is a transaction? 2. Th...
1. Why set maxPostSize? The tomcat container has ...
For evenly distributed layouts, we generally use ...
Use JS to implement a random roll call system for...
<br />In the page, typesetting is achieved b...
Nginx is now one of the most popular load balance...