View Full Version : clearInterval isnt stopping a function

02-28-2012, 12:18 AM
Im having some problem with stopping a function i run on loop using setInterval


var Cycle = setInterval('cycle()', 3000);

/* ... some other functions here ... */

var ID = $(this).attr('ID');
/* ... Rest of this function ... */
if (ID == 'Home')
var Cycle = setInterval('cycle()', 3000);

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

02-28-2012, 02: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.

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