简体   繁体   中英

setInterval works when alert is included in if conditions, else it works only once

this question might be seems to be the similar question asked previously but its not.

My question is following : I have code snippet as follows :

    var i = 0;              

    function func2() {                  
    if (i==0){

    document.getElementById('frame1').contentWindow.opts[selected].setAttribute('name','channels');
    var strValue = document.getElementById('frame1').contentWindow.opts[selected].getAttribute('name');
   //alert("Attribute value set :" + strValue);
   document.getElementById('frame1').contentWindow.handleKeyCode(VK_ENTER);
   i++;

   }
   else if(i==1){

   // For Channels section -- "Get current channel details"
   document.getElementById('frame1').contentWindow.opts[selected].setAttribute('name','get');
   var strValue = document.getElementById('frame1').contentWindow.opts[selected].getAttribute('name');
   //alert("Attribute value set :" + strValue);
   document.getElementById('frame1').contentWindow.handleKeyCode(VK_ENTER);
   i++;

   }
   else if(i==2){

   // For Channels section -- "Set current channel details"
   document.getElementById('frame1').contentWindow.opts[selected].setAttribute('name','set');
   document.getElementById('frame1').contentWindow.menuSelect(1);
   var strValue = document.getElementById('frame1').contentWindow.opts[selected].getAttribute('name');
   //alert("Attribute value set :" + strValue);
   document.getElementById('frame1').contentWindow.handleKeyCode(VK_ENTER);     
   i++;

   }
   }

   $(function() {
      setInterval(function(){func2();}, 1000);
   });

In this the setInterval works only once ie it executes only the first if condition. But as I enables the "alert" messages of all the conditions then the setInterval works fine.

I don't want the "Alert Messages" for this setInterval if conditions, it should executed each if elseif statment as per the condition and at given time interval.

I am enable to achieve this. Also i didn't find any satisfactory reference from the setInterval related questions in this forum. Please help.

Thanks.

I believe you are getting an exception inside func2 which make it seem like it is not running more than once. Reading .contenWindow when it is null. Try using try/catch to catch the exception. Or use a debugger.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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