javascript - executing method once if there is multiple instances -


i have question, have board 500 y's tall, , have falling block grabs y , moves own variable changes static y dynamic y y of falling block, if when block reaches 350 y on on board executes code in if statement once. issue having is doing each y under 350, way 500.

below small snippet of have, before default y set @ 135, , dy set -1; y set y-dy inside function, , block falling. in if statement checks if past point, , logs message, issue is, each point y past 351 logs message, want once, there way can use == or should set variable y or that.

here's code.

var y = y-dy; curval = 0; ctx.drawimage(block,x,y,50,50);  if(y > 351 && x < 64){ console.log('red!'); //set value of block here curval = curval + randnum; console.log(curval); } 

and if cant that, there way let event happen once?

declare global variable:

var flag = true; 

and use this:

if(y > 351 && x < 64 && flag){     flag = false;     ... 

this ensure if block executed @ once.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -