0

How to set a Variable from false to true?

I understand only a little in JavaScript, and I've been having troubles on how to set up an Auto Clicker system for this little game i made... this is the situation: i've used the setInterval() with a 1-second delay and for that to work, the variable autoClicker must be set to true first.. if you want, you could add a few suggestions on what improvement i should do with my mess.. 😅 thank you! https://code.sololearn.com/WpySlJRLfGhY/?ref=app

9th Apr 2017, 1:57 PM
Kyle Christian Orilla
Kyle Christian Orilla - avatar
8 Réponses
+ 1
@Kyle, ur setInterval is out side a function that's y it is not executing. put it inside a function (buyClicker) set autoClicker = true if points are greater than 9 if (autoClicker == true) { var ctr=0;// click counter var timer = setInterval(function() { document.getElementById("button").click(); ctr++;//increment if(ctr ==10) { clearInterval(timer) //stop interval after 10 clicks autoClicker = false; //reset auto clicker } }, 1000); } I'm not going to write whole code for u, I just wanted to help..
9th Apr 2017, 4:02 PM
Eranga
Eranga - avatar
+ 4
Another way to switch the value of a boolean is to write: var mySwitch = false; mySwitch = !mySwitch; Then if you repeat it you will get this result: mySwitch = false; mySwitch = !mySwitch; //True mySwitch = !mySwitch; //False mySwitch = !mySwitch; //True mySwitch = !mySwitch; //False etc...
9th Apr 2017, 2:35 PM
Geoffrey L
Geoffrey L - avatar
+ 1
var myVar = false
9th Apr 2017, 1:58 PM
koder
koder  - avatar
0
that's what i've already done, what I want to do is to set the variable to true after being called in a function..
9th Apr 2017, 2:03 PM
Kyle Christian Orilla
Kyle Christian Orilla - avatar
0
Suggestion: Don't use setInterval inside while loop. use if condition and clear interval when u want to stop the interval as it going to execute evey 1sec time. while (autoClicker == true) { setInterval(function() { document.getElementById("button").click(); }, 1000); }
9th Apr 2017, 2:07 PM
Eranga
Eranga - avatar
0
Wht I understood was u want to start timer if autoClicker​ is true and stop after a click. try this . if(autoClicker == true){ var timmer = setInterval(function() { document.getElementById("button").click(); autoClicker​ = false;//set autoClicker​ false clearInterval(timmer);//stop timmer }, 1000); }
9th Apr 2017, 2:12 PM
Eranga
Eranga - avatar
0
i appreciate all those who answered.. but none of them seemed to help solve my problem.. i did the autoClicker = !autoClicker; thingy, but it did not start the autoClicker though..
9th Apr 2017, 3:02 PM
Kyle Christian Orilla
Kyle Christian Orilla - avatar
0
@Eranga, thank you! putting it in the function DID work! i thought it was the boolean that was causing the problem! 😂
9th Apr 2017, 4:34 PM
Kyle Christian Orilla
Kyle Christian Orilla - avatar