I try to write a setTimeout function in javascript, and get some problem. When I use IE, I click the mouse right and show the context menu, the setTimeout stop working.

Here is my code.

var t = 30000;
function myTimer(){
    if(t <=0 ){
        console.log('STOP');
    }else{
        setTimeout(myTimer, 1000);
        console.log(t/1000);    
    }
    t-=1000;
};

Does anybody have this problem? Thanks.

2 Answers

0
Deepak-MSFT On Best Solutions

I made a test with your sample code and I am able to produce the issue.

Here is a testing result.

enter image description here

Based on my testing result, I find that issue is not related with setTimeout. I also tested with this JS Coundown code example and it produce the same issue.

Issue is that IE pause the execution of JS code while context menu is visible.

It looks like by design and I did not get any solution for it.

As a work around, you can try to disable the right click (only for IE) using JavaScript may help to avoid the issue.

0
Syed Mohamed Aladeen On

you have missed to call the timer function. try like this.

var t = 30000;
function myTimer(){
    if(t <=0 ){
        console.log('STOP');
    }else{
        setTimeout(myTimer, 1000);
        console.log(t/1000);    
    }
    t-=1000;
};

myTimer()