Jump to content

Clearinterval Not Working


mobone

Recommended Posts

Whats wrong here??

<script type="text/javascript">var bleed_timer_counter=0;var characters = new Array();	characters['opponent'][bleed_timer_counter]=setInterval('bleed_attack()', 1000);	setTimeout( function () {		clearInterval(characters['opponent'][bleed_timer_counter]);		document.write("done<br>");	},3001);		function bleed_attack() {document.write("hey<br>");}</script>

When the ['opponent'] is completely removed, making it only a one level array, the interveral timer gets cleared out, but if its two level, with the ['opponent'] key, it doesn't ever get cleared out.What the?

Link to comment
Share on other sites

That solved that problem, thanks. But now heres the real problem my other script is experiencing.Whats supposed to happen is set a timer, set the clear timer for the timer to end 3.001 seconds later, and do that two times. But its setting two timers, and clearing only one.When I change the 1st [bleed_timer_counter] to 0, it works. I still don't know whats wrong but I thought it might help.

<script type="text/javascript">var bleed_timer_counter=0;var characters = new Array();characters['opponent'] = {};	characters['opponent'][bleed_timer_counter]=setInterval('bleed_attack()', 1000);	setTimeout( function () {		clearInterval(characters['opponent'][bleed_timer_counter]);		document.write("done<br>");	},3001);		bleed_timer_counter=bleed_timer_counter+1;	characters['opponent'][bleed_timer_counter]=setInterval('bleed_attack2()', 1000);	setTimeout( function () {		clearInterval(characters['opponent'][bleed_timer_counter]);		document.write("done<br>");	},3001);	function bleed_attack() {document.write("foo<br>");}function bleed_attack2() {document.write("bar<br>");}</script>

Output:foobarfoobarfoobardonedonefoofoo...continues with foo...

Link to comment
Share on other sites

When you increment the interval number it's doing that globally, it doesn't have a different value in each function. You might want to use array.push to add intervals to an interval array, and array.shift to get them off and clear them. Push will add an item to the end of an array, and shift will remove an item from the front. So you have sort of a queue going, first-in-first-out.

	characters['opponent']['bleed_intervals'] = []; // empty array	characters['opponent']['bleed_intervals'].push(setInterval('bleed_attack()', 1000));	setTimeout( function () {		clearInterval(characters['opponent']['bleed_intervals'].shift()); // clear next interval		document.write("done<br>");	},3001);	characters['opponent']['bleed_intervals'].push(setInterval('bleed_attack2()', 1000));	setTimeout( function () {		clearInterval(characters['opponent']['bleed_intervals'].shift());		document.write("done<br>");	},3001);

Push and shift work directly on the array, so shift will remove the first element from the array, return it, and at the end the array will have one less element. Array.pop works like shift except it removes from the end of an array instead of the beginning. You could use push and pop to implement a stack, first-in-last-out. Shift and unshift remove and add elements to the beginning, and push and pop work on the end.

Link to comment
Share on other sites

Wow your great. Thanks!I still don't understand why it wouldn't work my way though. If its changing it globally, then it should have it within the function I thought. Why is it that the second function has it but the first one doesn't?

Link to comment
Share on other sites

It has the same value in both functions, it's using the global value for that counter. When you do this:

	characters['opponent'][bleed_timer_counter]=setInterval('bleed_attack()', 1000);	setTimeout( function () {		clearInterval(characters['opponent'][bleed_timer_counter]);		document.write("done<br>");	},3001);

It doesn't "save" a copy of the variable for use in that function, whenever the function runs it uses whatever the current value of that variable is.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...