The project has multiple modules that use the same polling logic, how to gracefully integrate together and can provide api for polling switches.

Page A/B/C shares the same data and needs to be polled. How to gracefully integrate multiple polls and provide controllable api

May.06,2022

do it based on the form of events. Npm events module is required

app.js, it is recommended to expose switch methods and listening event methods to the $vm prototype chain, so that each vue component can listen and switch

const EventEmitter = require('events').EventEmitter;
const ee = new EventEmitter();
let enableTimer = true;
let timer = setInterval(()=>{
    if(!enableTimer) {
        clearInterval(timer);
        timer = null;
        return;
    }
    ee.emit('ok'); // 
},60000);

/ / other JS

this.$ee.on('ok',()=>{}); // 
this.$ee.off('ok',()=>{}); // 
Menu