Before i tear my hair out and go down the wrong path in terms of debugging. Can someone confirm that this code will do as intended. Ie. animate 5 divs to different positions:
var i, j, k; $('#menuButton').click(function(){ for (i=1; j=0; k=150; i<=5; i++; j+=30; k-=30){ $('.spinner #item' + i).animate({ left: '+=' + j, bottom: '+=' + k }, 500, function() { // Animation complete. }); } });
When i click the #menuButton
, nothing happens and I get this error:
Uncaught SyntaxError: Unexpected token ; on the 'for()' line...
You can create two counters that are updated simultaneously in a for loop using the comma operator. Multiple let and var declarations can also be joined with commas.
And you, too, can now declare multiple variables, in a for-loop, as follows: Just separate the multiple variables in the initialization statement with commas. Do not forget to end the complete initialization statement with a semicolon.
Increment is an expression that determines how the loop control variable is incremented each time the loop repeats successfully (that is, each time condition is evaluated to be true). The for loop can proceed in a positive or negative fashion, and it can increment the loop control variable by any amount.
You've got some semicolons where you want commas:
for (i=1, j=0, k=150; i<=5; i++, j+=30, k-=30) { /* do work */ }
You should only have three "statements" inside your for
for( [init]; [test]; [increments]) { [block] }
To do multiple [inits]
or [increments]
you have to use the sometimes magical, but oft forgotten, comma operator
too much semicolons there
for (i=1; j=0; k=150; i<=5; i++; j+=30; k-=30){
should be
for (i=1, j=0, k=150; i<=5; i++, j+=30, k-=30){
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With