Vue使用輪詢定時(shí)發(fā)送請(qǐng)求代碼
一、什么是輪詢?
輪詢(Polling)是一種CPU決策如何提供周邊設(shè)備服務(wù)的方式,又稱“程控輸出入”(Programmed I/O)。輪詢法的概念是,由CPU定時(shí)發(fā)出詢問(wèn),依序詢問(wèn)每一個(gè)周邊設(shè)備是否需要其服務(wù),有即給予服務(wù),服務(wù)結(jié)束后再問(wèn)下一個(gè)周邊,接著不斷周而復(fù)始。 在WEB上來(lái)說(shuō)就是客戶端一直向服務(wù)端發(fā)起請(qǐng)求,服務(wù)端返回?cái)?shù)據(jù),不論返回什么都會(huì)再次向服務(wù)端發(fā)送請(qǐng)求。
二、采用定時(shí)器進(jìn)行輪詢
<template> <div></div></template> <script>export default { data() { return { num: 0 }; }, created() { // 實(shí)現(xiàn)輪詢 window.setInterval(() => { setTimeout(this.getNewMessage(), 0); }, 3000); }, methods: { // 請(qǐng)求是否有新消息 getNewMessage: function() { console.log('請(qǐng)求' + this.num++ + '次'); } }};</script> <style scoped></style>
效果:
補(bǔ)充知識(shí):vue 輪詢操作
創(chuàng)建一個(gè)彈出框
<div><el-dialog :visible.sync='loadingVisible' width='30%'><i class='fa fa-spin fa-star'></i><span>這是一段信息</span> </el-dialog> </div>
增加按鈕
<el-button type='text' @click='doing'>智能推薦</el-button>
關(guān)鍵js代碼
doing() {const vm = this;vm.loadingVisible = true;vm.timer = setInterval(vm.getstatus, 2000); }, getstatus() {const vm = this;vm.loadingVisible = false;clearInterval(vm.timer); } },
以上這篇Vue使用輪詢定時(shí)發(fā)送請(qǐng)求代碼就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
