JavaScript - Clearinterval Is No Worky.
So, I have a very simple script to show/hide a div on mouseover/onmouseout... basically, it's a slide-down menu, without the actual animation.... In any event, clearInterval doesn't seem to do a damn thing, and it's not a scope problem, and have tried it as many ways as I can think... still can't get a timer to clear. Have done a lot of searching, but seems like it typically boils down to a problem of variable scope, which I feel is not the case here.
Code: <script type="text/javascript"> var navClosedY = -60; var navOpenY = 0; var navObj; var hideTimer; //globally defined, should be accessible in all the functions. function showNavControl() { navObj = top.mainFrame.document.getElementById('navControl'); navObj.style.top = navOpenY + "px"; } function hideNavControl() { hideTimer = setInterval("hideNav()", 1000); //never clears. } function hideNav() { navObj.style.top = navClosedY + "px"; clearInterval(hideTimer); //this is the bit that doesn't do anything. alert(hideTimer); //this works, so I KNOW it can access hideTimer. } </script> And, in the DOM, I just have a div like this: Code: <div id="navControl" class="navControlMain" onmouseover="showNavControl();" onmouseout="hideNavControl();"> Similar TutorialsI am trying to have a slide show of pictures that are randomly generated stop when the user mouses over the image, I have got everything to work so far except the onmouseover event, even the onmouseout seems to work I don't necessarily need the exact answer but any guidance would be appreciated! <script type="text/javascript"> /* ! [CDATA[ */ setInterval('randomImage()', 4000); //sets the interval of images to show every 4 seconds and runs the function to do so. var imgs = new Array("outdoors.jpg", "kayak.jpg", "fishing.jpg", "scuba.jpg", "biking.jpg"); //the array of images function randomImage() { document.getElementById("image").src = imgs[Math.floor(Math.random()*imgs.length)]; } //the function calls for a random number and applies it to the array of images to show a random picture. /* ]]> */ </script> </head> <body> <p> <img src="outdoors.jpg" width="200" height="200" alt="Random Image" id="image" onmouseover="clearInterval();" onmouseout="randomImage()" /> </p> I am not quite sure what I am doing wrong I've tried mutliple different set ups for the "onmouseover="clearInterval();" and nothing seems to work. Hello, I am trying add some animation effects to a navigation button and am having some problems with my code. I am using setInterval(); and the problem came with trying to clear it using clearInterval(); if i have this code then it seems to clear it fine: Code: var i = 21; function w(){ document.getElementById("homebtntop").style.height = i + "px"; i--; } function c(){ if(i<=6){ window.clearInterval(t); } } function r(){ t = self.setInterval("w()",15); } function slideIn(){ t = self.setInterval("w()",15); clearInterval(t); } but if i then try to call it from inside a function c(); it doesn't work: Code: var i = 21; function w(){ document.getElementById("homebtntop").style.height = i + "px"; i--; } function c(){ if(i<=6){ window.clearInterval(t); } } function r(){ t = self.setInterval("w()",15); } function slideIn(){ t = self.setInterval("w()",15); clearInterval(t); } Any ideas? i am sorry if my code makes no ssense or is badly formatted, this is my first time with javasript. Thank you. Hi, all. I am very new to Javascript (and coding generally); the code below is part of my first project. I teach, and just built a flash card / timed-drill site for my students which has a number of setIntervals going on as a central part of its timed functionality. I ran into a problem when trying to create controls that would allow adjustments in the drill timing within the same page. The running setIntervals simply need to be killed when a variable is changed through the user form input, but I cannot seem to do this through any means known to me. I've tried about 15 different approaches and none of them work. Here is one function in my code that runs a setInterval and refuses to be killed by any external stimuli (like, say, a specific killerVariable value): Code: function countdownTimer() { var countdown=timeLimit/1000; var countdownString=countdown+''; document.getElementById("countdownTimerArea").innerHTML = countdownString; var killCountdown=setInterval(function() { countdownString=(countdown-1)+''; document.getElementById("countdownTimerArea").innerHTML = countdownString; countdown--; if (countdown==0) { clearInterval(killCountdown); } },1000); } The clearInterval shown in the code *does* work correctly. However, if I try to set up another conditional, killerVariable-value-driven clearInterval in this function (or even change the above to if (countdown==0 || killerVariable==1), it does not stop the setInterval when killerVariable changes. In the end, because I needed this to go online quickly, I "solved" this problem by simply creating a few different versions of the page with hard-coded variable values and having the form control buttons take the user to those pages rather than trying to stop and restart the timed processes within the same instance of the script. However, this was a real kludge of a solution, and I am still very curious as to what I could have done to actually make this work. Is there anything I could have done short of totally rewriting the function as a loop with setTimeout? Alternately-- and this *does* seem incredibly basic, but I can't get an answer no matter how I phrase my Google and CF queries-- is there some way to hard-kill a currently running function via another function in Javascript? I think that would have solved my problems. Im having some problem with stopping a function i run on loop using setInterval Code: $(document).ready(function(){ var Cycle = setInterval('cycle()', 3000); /* ... some other functions here ... */ $('.Test').click(function(){ var ID = $(this).attr('ID'); clearInterval(Cycle); /* ... Rest of this function ... */ if (ID == 'Home') { var Cycle = setInterval('cycle()', 3000); } console.log(ID); }); }); When i click a link with the Test class it runs through the function but the loop never stops and im not sure why, cant anyone help me solve this? If you want to see the bits of JS i cut out just say but i dont think there relevant. EDIT: Seems to be the if (ID == 'Home') bit which is causing the problems even though when in the log it shows ID being the word 'Live'. Have i missed some stupid obvious thing ? Whenever i click I have a script that works until I add the clearInterval command, and I am not sure what I have done wrong. The code is: Code: <script type="text/javascript"> function loadChatTalkRefresh(File,ID,Msg){ var xmlhttp; if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } else { try{ xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } } xmlhttp.onreadystatechange=function(){ if (xmlhttp.readyState==4 && xmlhttp.status==200){ document.getElementById(ID).innerHTML=xmlhttp.responseText; empress = setInterval(function(){loadChatTalkRefresh(File,ID,Msg)},3000); willow = clearInterval(empress,210000); } } var params=Msg; xmlhttp.open("POST",File,true); xmlhttp.setRequestHeader("Pragma", "Cache-Control:no-cache"); xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xmlhttp.setRequestHeader("Content-length", params.length); xmlhttp.setRequestHeader("Connection", "close"); xmlhttp.send(params); } </script> |