mobone Posted June 11, 2009 Share Posted June 11, 2009 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 More sharing options...
justsomeguy Posted June 11, 2009 Share Posted June 11, 2009 Define characters['opponent'] before defining characters['opponent'][bleed_timer_counter]. Link to comment Share on other sites More sharing options...
mobone Posted June 11, 2009 Author Share Posted June 11, 2009 I tried var characters['opponent'] = new Array(); but IE says it expected a ; Define characters['opponent'] before defining characters['opponent'][bleed_timer_counter]. Link to comment Share on other sites More sharing options...
justsomeguy Posted June 11, 2009 Share Posted June 11, 2009 It doesn't like the "var". You're defining a property on an object, not a new variable. You can even just do this:characters['opponent'] = {}; Link to comment Share on other sites More sharing options...
mobone Posted June 11, 2009 Author Share Posted June 11, 2009 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 More sharing options...
justsomeguy Posted June 11, 2009 Share Posted June 11, 2009 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 More sharing options...
mobone Posted June 11, 2009 Author Share Posted June 11, 2009 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 More sharing options...
justsomeguy Posted June 11, 2009 Share Posted June 11, 2009 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.