简体   繁体   English

Javascript中如何运行和停止多个Intervels

[英]How to run and stop multiple Intervels in Javascript

Suppose i am calling 3 interval with time 500ms,1s,1.5s.假设我用 500 毫秒、1 秒、1.5 秒调用 3 个间隔。 once i click on 500ms button that time i need to stop other 2 interval run only 500ms.一旦我点击 500ms 按钮,我需要停止其他 2 个间隔运行仅 500ms。 Like i click on 1s then stop previous interval that is 500ms.就像我点击 1s 然后停止之前的 500ms 间隔。 How i figure it out.我怎么弄明白。

socket.on("interval-1",(value)=>{
        console.log(value);
        if(value==1){
           
            var timer1  = setInterval(function(){
                let price = Math.floor(Math.random() * 100);
                socket.emit("price",price)
               
            },500);
            
        }
        
        else if(value==2){
           
           var timer2  = setInterval(function(){
                clearInterval(timer1)
                let price = Math.floor(Math.random() * 100);
                socket.emit("price",price)
                
            },1000);
        }
        else if(value==3){
          
            setInterval(function(){
                clearInterval(timer1)
                clearInterval(timer2)
                let price = Math.floor(Math.random() * 100);
                socket.emit("price",price)
                
            },1500);
        }
        
    })

I tried it but once i interval it started it is not stoping when i click on run other intervals.我试过了,但是一旦我开始间隔,当我点击运行其他间隔时它就不会停止。

Thanks in Advance!提前致谢!

Your timer1 and timer2 variables are local to one execution of the socket.on() handle so when a subsequent handler comes, you can't access the previous variables.您的timer1timer2变量是socket.on()句柄的一次执行的本地变量,因此当后续处理程序到来时,您无法访问先前的变量。 You can fix this by declaring the timer variables at a higher scope so the same variable is accessible on subsequent socket.io messages.您可以通过在更高的 scope 处声明计时器变量来解决此问题,以便在后续的 socket.io 消息中可以访问相同的变量。 I'd suggest an implementation like this:我建议这样的实现:

let previousTimer;

socket.on("interval-1", (value) => {
    console.log(value);
    // clear any previous interval timer so it doesn't keep going forever
    if (previousTimer) {
        clearInterval(previousTimer);
        previousTimer = null;
    }
    if (value == 1) {
        previousTimer = setInterval(function () {
            let price = Math.floor(Math.random() * 100);
            socket.emit("price", price)
        }, 500);
    } else if (value == 2) {
        previousTimer = setInterval(function () {
            let price = Math.floor(Math.random() * 100);
            socket.emit("price", price)
        }, 1000);
    } else if (value == 3) {
        previousTimer = setInterval(function () {
            let price = Math.floor(Math.random() * 100);
            socket.emit("price", price)
        }, 1500);
    }
});

Notes: The variable I declared called previousTimer in this implementation is a module-level variable, not a global variable.注意:我在这个实现中声明的变量previousTimer是一个模块级变量,而不是全局变量。 It is accessible only to the code within this module.它只能由该模块中的代码访问。

Also, you should stop using var .另外,您应该停止使用var Modern Javascript should use let or const , not var .现代 Javascript 应该使用letconst ,而不是var

You also generally want to use === for comparisons, not == .您通常还希望使用===进行比较,而不是== If the value variable is actually a number, not a string, then you should use === to compare it.如果value变量实际上是一个数字,而不是一个字符串,那么你应该使用===来比较它。 The comparison with == will attempt to do type conversion, allowing a number to be equal to a string which can lead to very unpredictable results in your code.==的比较将尝试进行类型转换,允许数字等于字符串,这可能会导致代码中出现非常不可预测的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM