javascript - Loop not stopping or not starting -


i making small userscript survey site. whenever click survey , indicated did not receive script supposed refresh amount of times trying claim survey until gives up. (if not know userscript is, think of stays running time on specific website, can control things using javascript selectors , stuff)

this script far (the javascript portion problem in):

var elementexists = document.getelementsbyclassname("message warning")[0];      if(elementexists)  {      var attempts = 0;      while(attempts<5)      {          attempts += 1;          location.reload();          if(elementexists)          {              //nothing          }          else          {              window.stop();          }        }      window.stop();  }

this first time using javascript assumed reason errors, after 45 minutes of debugging baffled. if remove last "window.stop();" code refreshes webpage infinitely. if stays there code doesn't start. seems if while loop being skipped if "window.stop();" present. javascript does, or problem elsewhere?

if lead me in right direction or me fix grateful!

(also checked selector see if issue, have done correctly)

update: turns out location.reload(); stops script , forces reload. since creating userscript realized use greasemonkey apis (or more stumbled upon). using gm_setvalue , gm_getvalue able work around problem , script reloaded amount of times (depending on variable tries) , stopped when finished. after messing around bit, reverting older version script, script doesn't doesn't execute @ anymore; "counter < tries" seems false reason... figure out why? if documentation needed: https://wiki.greasespot.net/gm_getvalue https://wiki.greasespot.net/gm_setvalue

var tries = 5;  var elementexists = document.getelementsbyclassname("message warning")[0];  var counter = gm_getvalue('counter', 0);    if(elementexists && counter < tries)  {      gm_setvalue('counter', ++counter);      location.reload();  }

(both counter , tries seem integer values.. there should in problem in comparing them...)

also suggested @yuriy636 attempted reset variables , created this

var tries = 5;  var elementexists = document.getelementsbyclassname("message warning")[0];  var counter1 = gm_getvalue('counter1', 0);    if(elementexists && counter1 < tries)  {      gm_setvalue('counter1', ++counter1);      location.reload();  }    if(elementexists && counter1 == tries)  {      gm_deletevalue('counter1');      window.close();  }    if(!!elementexists)  {     gm_deletevalue('counter1');      return;      alert("stops script while hidden");  }

but again hit infinite loop.. rip

update 2: not rip afterall... solution:

var tries = 50;  var elementexists = document.getelementsbyclassname("message warning")[0];  var counter = gm_getvalue('counter', 0);    if(elementexists && counter < tries)  {      gm_setvalue("counter", counter + 1);      location.reload();  }  else  {   gm_deletevalue("counter");  }  if(elementexists && counter >= tries)  {   window.close();  }

100% working, after indicated amount of tries, if error still exists page closed

the problem have location.reload() in while loop. causes page refresh before interesting happens in loop. in particular code expect page refresh seemingly infinitely because every time page refreshes, refresh again.


Comments