if statement - jQuery - clearInterval seems not to work -


long story short: if interval count exceeds 5 code should clear interval, stopping executing function every second. doesn't seem work.

html

<div id='feedback'></div> 

javascript / jquery

window.intervalcount = 0;  var interval = setinterval(function () {     intervalcount += 1;     $("#feedback").text(intervalcount); }, 1000);  if(intervalcount > 5) {     clearinterval(interval); } 

but can't seem find issue...

so currently, code going run order:

window.intervalcount = 0;  // interval defined here var interval = setinterval(function () {     intervalcount += 1;     $("#feedback").text(intervalcount); }, 1000);  // 0 still if(intervalcount > 5) {     clearinterval(interval); }  // 1 second after interval defined:     intervalcount += 1; // equals 1     $("#feedback").text(intervalcount);  // 2 seconds after defined:      intervalcount += 1; // equals 2     $("#feedback").text(intervalcount);  // etc, forever 

placing the:

if(intervalcount > 5) {     clearinterval(interval); } 

inside interval's function perform desired behavior.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -