+ 1
Script isn't working?
I have been attempting to create a loading sign using javascript and html, but for some reason this html won't work: <!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <p id = "loading"> Loading 0% </p> <button onclick = "loading()"> Begin Loading </button> </body> <script> var loadsign = 0 function loading() { setTimeout( while (loadsign < 100) var load = document.getElementById ("loading").innerHTML = "Loading " + loadsign + "%"; loadsign++ ), 500; loading() } </script> </html> Any suggestions?
5 odpowiedzi
+ 7
the bracket for setTimeout is ended early, place it after the '500'. setTimeout will execute it after 500 miliseconds, now you have infinite recursion (though forgot a semi-colon on the function call) constantly doing that, and re-setting everything. I believe what you're look for is setInterval() which will constantly repeat something (check it out).
https://www.w3schools.com/jsref/met_win_setinterval.asp
+ 6
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<p id = "loading"> Loading 0% </p>
<button onclick = "loading()"> Begin Loading </button>
</body>
<script> var loadsign = 0;
var load = document.getElementById ("loading");
function loading() {
var timer = setInterval(function(){
load.innerHTML = "Loading " + loadsign + "%";
loadsign++;
if(loadsign > 100){
clearInterval(timer);
}
}, 500);
}
</script>
</html>
+ 5
My last comment is a working code. You can try.
Things to note:
1) While not needed when using setTimeout
2) Check syntaxes for setTimeout
3) getElementById need to be done outside only once.
4) Since you want the function to be called repeatedly after every half a sec so you can use setInterval rather than setTimeout.
+ 1
Thanks for the help! Its working now 😃
0
I am new to getElementById so I am a noob 😂