Settimeout function? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Settimeout function?

So I have been trying to figure out how to set a cooldown in javascript, and for some reason this code won't work for me: settimeout(function() { document.write("Hello World."); }, 500); Any help is appreciated!

5th Apr 2017, 9:47 PM
iBrandon
iBrandon - avatar
16 Answers
+ 1
Thank you so much!
5th Apr 2017, 10:02 PM
iBrandon
iBrandon - avatar
+ 1
@iBrandon 1.It's registry sensitive. 2.You don't realy need ";" if you're using just 1 statement in function (but it doesn't realy matter,it's a good practice to always use it) so...just always remember first one :D
5th Apr 2017, 10:05 PM
Rose Sevenyears
Rose  Sevenyears - avatar
+ 1
One more question, I have been working on a loading sign in javascript lately, is it possible you could tell me why nothing shows up when I run it? I know its not complete but Im figuring it should at least run what I have properly unless Im doing something wrong. Putting script in next response:
5th Apr 2017, 10:11 PM
iBrandon
iBrandon - avatar
+ 1
var loading = 0 while (loading <= 100) { setTimeout(function() { document.write( + loading + "%"); loading++ } }
5th Apr 2017, 10:12 PM
iBrandon
iBrandon - avatar
+ 1
Alright! Tysm for the help / advice!
5th Apr 2017, 10:26 PM
iBrandon
iBrandon - avatar
+ 1
EDIT: Ok, so I modified what you told me to and it still won't show anything. This is what the script looks like: var loading = 0; setTimeout(function() { while (loading <= 3) { document.write(loading + "%"); loading++; } ,500)} Is there anything I did wrong?
5th Apr 2017, 10:39 PM
iBrandon
iBrandon - avatar
+ 1
Thanks for all the help so far, however whenever I attempt to write the code that you gave me as javascript, nothing will show up D: This is what I am writing into the javascript (in case I did a typo) var loading = 0; var prc = setInterval (function(){ if (loading <= 100){ console.log(loading + "%"); loading++;} else {clearInterval(prc);} }, 1000) Is it just because I am attempting to use the script in this app that it won't work?
5th Apr 2017, 10:52 PM
iBrandon
iBrandon - avatar
+ 1
Ok, so your latest script seems to work, however the percentage being written has no cooldown, meaning it will go from 0-100% without any time interval. Does this only occur for my device or does it do the same for yours, and do you know if there is a way to fix this? EDIT: I figured out why it was doing this, the timeout only controls the amount of time taken until all of the numbers appear on the screen. Do you know if there is a way to have the time interval count for every percentage / loop?
5th Apr 2017, 11:40 PM
iBrandon
iBrandon - avatar
+ 1
Yup. Do you know if there is any way to fix this?
5th Apr 2017, 11:49 PM
iBrandon
iBrandon - avatar
+ 1
Ok so I tried your switch while with it recommendation and used this script, however whenever I run it it becomes stuck at 0%: var loading = 0; setTimeout(function() { if (loading <= 3) { document.write(loading + "% <br>"); loading++; } else document.write("Done!"); },500) EDIT: Perhaps I can try making an if statement within a while statement? Im basically trying random stuff to see what works and what doesn't at this point xD
6th Apr 2017, 12:06 AM
iBrandon
iBrandon - avatar
+ 1
Cool, that method works! I guess I'll need to learn more HTML / DOM if I am to become a better programmer 😝 Thanks for the help!
6th Apr 2017, 12:32 AM
iBrandon
iBrandon - avatar
+ 1
Depends who you realy want to be ... for web you'll need css+html+js+jq+php+...other stuff...just for start :D P.s. useless comments(and not working things) removed in case other learners will read it.
6th Apr 2017, 12:37 AM
Rose Sevenyears
Rose  Sevenyears - avatar
+ 1
Just tryed :) var loading = 0; var prc = setInterval (function(){ if (loading<=100){ console.log(loading + "%"); loading++;} else {clearInterval(prc);} },1000) Little update with removing interval (to not trash memory)... To check result you need console...
6th Apr 2017, 12:38 AM
Rose Sevenyears
Rose  Sevenyears - avatar
+ 1
Guess problem in document.write method ...or maybe I'm too sleepy to guess what's the problem :D Cause anytime I try to change my old working timer with console- it doesn't work :D On practice you will not use document.write too anyway... var loading = 0; var prc = setInterval(function() { if (loading <= 5) { document.getElementById('test').innerHTML += (loading +"% <br>") ; loading++; } else { clearInterval(prc); } }, 1000) You can try this one :) You will not use this one too...cause it's realy badass thing,but at least it works :D To let it work you need to create <p id="test> </p> html element anywhere on your page. You'll learn it when you'll start learning DOM elements (but better to use Jquery library for it)
6th Apr 2017, 12:39 AM
Rose Sevenyears
Rose  Sevenyears - avatar
+ 1
Thanks for the info! I'll be sure to try it out! :D
6th Apr 2017, 12:46 AM
iBrandon
iBrandon - avatar
+ 1
Also... if you want to see changing numbers only : var loading = 0; var prc = setInterval(function() { if (loading <= 100) { document.getElementById('test').innerHTML = (loading + "%"); loading++; } else { clearInterval(prc); } }, 50) and if you wanna make somethin' more interesting and usefull, like % bars check this links: https://kimmobrunfeldt.github.io/progressbar.js/ https://www.w3schools.com/howto/howto_js_progressbar.asp
6th Apr 2017, 12:49 AM
Rose Sevenyears
Rose  Sevenyears - avatar