I m new to javascript and I m little bit confused on how does a webpage read the actual script.
let's say I have the folowing code:
<script>
vrijeme = document.getElementById("Label7").innerText;
minute = vrijeme.substring(0, 2);
sekunde = vrijeme.substring(3, vrijeme.length);
alert("fuck you");
var start_time=setInterval(secondFunction, 1000);
function secondFunction()
{
sekunde = sekunde - 1;
if (sekunde < 0)
{
minute = minute - 1;
sekunde = 59;
}
if (minute <0)
{
clearInterval(start_time);
}
if (sekunde < 10) { document.getElementById("Label7").innerText = minute + ":0" + sekunde; }
else { document.getElementById("Label7").innerText = minute + ":" + sekunde; }
if (minute < 10 && minute < parseInt(vrijeme.substring(0,2))) { document.getElementById("Label7").innerText = "0"+minute + ":" + sekunde; }
}
</script>
and I add the function to my button onclick event ...
I noticed that as soon as I opened the browser the SCRIPT RAN! So i suppose the script runs automatically as the webpage opens?
The other question that is bothering is related to the above code. The setInterval function reffers to the actual function that is written. How does the browser see the public variables declared in the script since the setInterval() function just points to the written function but not the code that is above the function (the variables ect...). I hope I was clear enough.
It would be great if you can recommend an article about .js execution on a web page.