+ 1
Help me understand this code please?
So I got this code from somebody else. I want to be able to understand the Javascript. I understand the basics, but I don't understand why some variables are there, and I don't understand that if/else statement. https://code.sololearn.com/Wt3WHVtOF4ZT/?ref=app
7 Answers
+ 2
// Created by Daniel Cooper
// Created by Ben Allen (Njinx)
window.onload = function() {
//happens if the window is loaded
var info = document.getElementById("print");
//the numbers will stay in this element
var toggle = document.getElementById("toggle");
//the button's id
var i = 0;
//the number to be writed
var btn = 1;
//0=number will increase 1=do nothing
interval = null;
//the interval for increasing the number
toggle.onclick = function () {
//happens when you click the button
if (btn) {
//1 is true 0 is false (first time its true)
btn = 0;
//sets btn to 0
interval = setInterval(function() {
//makes an interval. used to repeat
//continueally until something
//stopped the interval
info.innerHTML = i++;
//writes i and then adds 1
}, 1);
//1 means every 1 millisecond
}
els
+ 1
Thanks, but can you explain that if statement?
if (btn) {
}
I'm assuming it's testing if it's true? Correct me if I'm wrong.
+ 1
i said in the comment that its true when btn = 1, and false when btn = 0, but im not sure since i most of the time use [var name]{operant}(value to test){function}
+ 1
Roel Can confirm its testing if it's true. btn set to 1 will be true. So I'm basically testing if the btn is equal to 1.
0
explain using comments please.
0
you can copy paste it in your javascript but my internet connection failed
0
Ben Allen (Njinx) so i was right with my comment âș good to know