Javascript - display incrementing number every second -
i trying display different incremented number every sec can't setinterval thing right.
here have
function counter() { var = 0; while ( < 100 ) { // block executed 100 times. setinterval(console.log( 'currently @ ' + ), 1000); i++; // increment } } // end
but console.log firing 100 times, repeating.
thanks help.
mike
when create setinterval once, automatically phone call function (first argument) every 1000
milliseconds (second argument). don't need within while, set incrementing of i
within function (first argument).
function counter() { var = 0; // block executed 100 times. setinterval(function(){ if (i == 100) clearinterval(this); else console.log( 'currently @ ' + (i++) ); }, 1000); } // end
setinterval
update 1function counter() { var = 0; var funcnamehere = function(){ if (i == 100) clearinterval(this); else console.log( 'currently @ ' + (i++) ); }; // block executed 100 times. setinterval(funcnamehere, 7000); funcnamehere(); } // end
javascript
No comments:
Post a Comment