5 Replies - 11455 Views - Last Post: 03 August 2009 - 04:59 PM

Topic Sponsor:

#1 AlienWebguy  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 84
  • Joined: 04-March 09

setInterval working, clearInterval not

Posted 29 May 2009 - 01:51 PM

Hey guys. Basically what I'm trying to do here is execute a function every 100 milliseconds for 1500 milliseconds.

The function whenCompleted(); is executed after an ajax call is complete.


function whenCompleted(){

	var intVal = 0;

	intVal = setInterval ( function(){
			sIFR.replace(verlag, {
				selector: 'h2',
				css: '.sIFR-root { color: #000000; }',
				wmode: 'transparent'
			});
		 }, 100 );

	if(intVal)
		setTimeout ("clearInterval(intVal)", 1500 );

}





When the page is loaded, it indeed calls the function every 100 milliseconds as it should, but then 1500 milliseconds later instead of clearing the interval it spits out the error: intVal is not defined.

Am I missing something? It obviously acknowledges intVal in the if(intVal) so why doesn't it acknowledge it in the next line?

--S--

Is This A Good Question/Topic? 0
  • +

Replies To: setInterval working, clearInterval not

#2 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 114
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: setInterval working, clearInterval not

Posted 29 May 2009 - 07:06 PM

Probably has something to do with clearing the interval deleting the variable. I would set the interval, then set a timeout to stop the interval:
setTimeout( function() { clearInterval(intVal); }, 1500 );


:D
Was This Post Helpful? 0
  • +
  • -

#3 ahmad_511  Icon User is offline

  • MSX
  • member icon

Reputation: 110
  • View blog
  • Posts: 686
  • Joined: 28-April 07

Re: setInterval working, clearInterval not

Posted 30 May 2009 - 12:20 PM

hello,
the error you got 'intVal' is undefined is the key to solve the problem.

simply, all this is about variable's scope.

setTimeout can't access intVal scope, because you defined intVal inside whenCompleted
but when the time out comes you're totally out of that function scope (you can think about it as another function that you will call at a specific time)

so, you have two choices:
1- define intVal out of the function's scope, by moving it out of the function or simply remove the var Statement

2- assign intVal to the function using this Statement
this.intVal=0


and then you can refer to it like this
setTimeout ("clearInterval(intVal)", 1500 );



hope it helps

This post has been edited by ahmad_511: 30 May 2009 - 12:27 PM

Was This Post Helpful? 0
  • +
  • -

#4 SoLi  Icon User is offline

  • andydust.com

Reputation: 41
  • View blog
  • Posts: 1,438
  • Joined: 27-January 02

Re: setInterval working, clearInterval not

Posted 30 May 2009 - 12:44 PM

Hey, seeing as javascript all runs synchronously on a single thread you can just do this with the single setInterval and drop the additional setTimeout (unnecessary overhead):

here's what i'd do:
function whenCompleted(){

	var INT = 100;
	var MAX = 1500;
	var maxCount = MAX / INT;
	var i = 0;

	var myInterval = setInterval(function() 
	  {
		if(i < maxCount) {
		  doSifr();
		  i++;
		} else {
		  clearInterval(myInterval);
		};
	  }, INT );
}
function doSifr() {
  sIFR.replace(verlag, {
	  selector: 'h2',
	  css: '.sIFR-root { color: #000000; }',
	  wmode: 'transparent'
  });
};


This post has been edited by SoLi: 30 May 2009 - 12:44 PM

Was This Post Helpful? 0
  • +
  • -

#5 AlienWebguy  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 84
  • Joined: 04-March 09

Re: setInterval working, clearInterval not

Posted 30 May 2009 - 02:10 PM

*facepalm - of course I didn't even acknowledge clearInterval being its own function and being out of the variable scope of intVal. That being said, thank you to ahmad_511 for your solution to remove the "var" declaration, and thank you to SoLi for your much-improved version of what I was trying to do - your code works like a charm!

--Sean
Was This Post Helpful? 0
  • +
  • -

#6 Tiron  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 26
  • Joined: 03-October 08

Re: setInterval working, clearInterval not

Posted 03 August 2009 - 04:59 PM

Just go like this:

setTimeout ("clearInterval(" + intVal + ")", 1500 );



This places the number from intVal into the string so it clears the right thing even if you can't get intVal.

You guys are getting all technical with your loops and whatnot...
If I'm wrong and this won't work please correct me, as I'm not very big into Javascript.

This post has been edited by Tiron: 03 August 2009 - 05:05 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1