dcsimg
www.webdeveloper.com
Results 1 to 1 of 1

Thread: [b]setInterval, clearInterval gives unwanted loop

  1. #1
    Join Date
    Oct 2008
    Location
    moreno valley, California ,usa
    Posts
    161

    [b]setInterval, clearInterval gives unwanted loop

    Hey gang,
    My use of the setInterval and clearInterval functions keeps giving me an unwanted loop which jams the program up. I've stripped away all but the essentials on the code so you can see the problem.
    If you see the code below, after the condition clipht =>300, the else portion exec. and I get the alert "you have reached 300" BUT, the problem is the program executes one more call to rolldown() even though the timer is cleared!!?

    I put in comments to draw your attention:

    <script type="text/javascript" >

    //NOTE VARIABLES DELCLARED OUTSIDE OF FUNCTIOINS

    var active_menu=null;
    var targetmenuid=null;
    var clipht = 10;
    var timerID;
    window.onload=function()
    {

    //MM_preloadImages('buttons/ovcat_1.png','buttons/ovcat_2.png');
    init();
    }

    function init()
    {
    //window.alert("inside function init");
    var men_array=new Array();
    var allelems=document.getElementsByTagName("*");
    //window.alert("length of allelems" + allelems.length);

    for(var i=0; i<allelems.length; i++ )
    {
    if(allelems[i].className == "menu")
    {

    men_array.push(allelems[i]);
    }

    }

    //window.alert("the men_array length is:" + men_array.length);
    for(var j=0; j< men_array.length; j++)
    {
    men_array[j].onmouseover = change_menu;
    men_array[j].onmouseout = closeoldmenu;
    }

    }

    //THIS WORKS, ON MOUSE OVER, change_menu IS CALLED

    function change_menu()
    {
    window.alert("inside change_menu");

    closeoldmenu();
    targetmenuid=this.id + "list";
    active_menu=document.getElementById(targetmenuid);
    active_menu.style.position="absolute";
    active_menu.style.top= "120px";
    active_menu.style.left= "10px";
    active_menu.style.clip="rect(0px 300px 10px 0px)";
    active_menu.style.display ="block";

    //HERE IS THE SET UP FOR THE INTERVAL

    timerID=setInterval("rolldown()", 1);

    }
    function closeoldmenu()
    {
    //window.alert("you are inside closeoldmenu");
    if(active_menu)
    {
    active_menu.style.display = "none";
    active_menu = null;
    }
    }

    //HERE IS THE rolldown() FUNCTION THAT IS CALLED BUT ONCE TOO OFTEN!!

    function rolldown()
    {
    window.alert("inside rolldown");
    clipht=clipht + 50;
    if(clipht <300)
    {
    //THIS ALERT SHOWS UP ON EACH LOOP

    window.alert("inside clipht addition");
    // active_menu.style.clip="rect(0px 300px" + clipht + "px 0px)";
    }
    else {
    clearInterval(timerID);
    clipht = 0;
    //HERE IS THE STATEMENT THE FIRES WHEN clipht =>300

    window.alert("you have reached 300");
    }

    }//end rolldown

    </script>

    What happens is I get the "you have reached 300", then I get one more "inside clipht addition" and then the program just stops!
    I would appreciate any suggestions.

    captsig
    Last edited by captsig; 09-07-2011 at 01:16 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles