Results 1 to 2 of 2

Thread: clearInterval not erasing active setInterval

  1. #1
    Join Date
    Jul 2008

    clearInterval not erasing active setInterval

    I have a option/select dropdown menu with an onchange to run the code below to loop through an display an array of images .75" apart :

    <script type="text/javascript">
    function loadImage(){
      var time = document.getElementById('times').value;
      var plot = document.getElementById('plots').value;
      var plots = plot.split("_");
      var imgsrc= plot + time + ".gif";
      var html = "<center><img src=\"" + imgsrc + "\" alt=\"" + imgsrc + "\" /></center>";                                
      if(time != "loop"){
        document.getElementById('image').innerHTML =  html;
       if (time == "loop") {
        document.getElementById('image').innerHTML =  'Loading...';
    	var looper = {
    		il: [plot+"_03.gif",plot+"_06.gif",plot+"_09.gif",plot+"_12.gif",plot+"_15.gif",plot+"_18.gif",plot+"_21.gif",plot+"_24.gif",plot+"_27.gif",plot+"_30.gif",plot+"_33.gif",plot+"_36.gif",plot+"_39.gif",plot+"_42.gif",plot+"_45.gif",plot+"_48.gif"],
    	    curIdx: 0,
    	    change: function()
    			var curimg = looper.il[looper.curIdx];
    	        document.getElementById('image').innerHTML = "<center><img name=il id=il src=\"" + curimg + "\" alt=\"" + curimg + "\" /></center>";    
    	        looper.curIdx = looper.curIdx+1;
    			looper.curIdx = looper.curIdx % 16;
    	var loopsi = setInterval(looper.change, 750);
    I'd really like to kill the setInterval as soon as onChange (or the function above) is activated again. Right now, they just stack, and before long the looping image is looping through several different arrays at once, rather than just the latest. Could someone help? I'm struggling with this.

  2. #2
    Join Date
    Jan 2011
    United Kingdom, London
    var loopsi = setInterval(looper.change, 750);
    change it to:
    setTimeout(looper.change, 750);
    The way it is now it'll create another interval each time it goes to loop, although you probably weren't able to clear interval because of variable scope, shout if you really want to use interval (not recommended)

    BTW where do you change time to != "loop"? Isn't that neverending loop?
    Last edited by maurycy; 02-01-2013 at 11:33 AM.
    Javascript, jQuery / Prototype & PHP expert ready to complete your work on Freelancer.co.uk - Hire Me at https://www.freelancer.co.uk/users/1...&action=hireme

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