...

View Full Version : clearInterval isnt stopping a function



tomharto
02-27-2012, 11:18 PM
Im having some problem with stopping a function i run on loop using setInterval


$(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 :p?

Whenever i click

felgall
02-28-2012, 01:12 AM
EDIT: Seems to be the if (ID == 'Home') bit which is causing the problems

Remove the 'var' from in fromt of Cycle within that if statement. It is creating a separate Cycle variable that is local to $('.Test').click(function() and therefore losing access to the Cycle defined in $(document).ready(function() so thatthe first time you try to run clearInterval it is referencing the wrong Cycle field.

tomharto
02-28-2012, 06:30 AM
Thanks a lot mate :) works like a treat now!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum