Yo Guyz I forgot how to use the setintervals () function in JavaScript help me | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

Yo Guyz I forgot how to use the setintervals () function in JavaScript help me

9th Jul 2017, 9:24 AM
Ishaq Za'rour
13 Answers
+ 11
setInterval(callback, milliseconds) Callback can be specified inside: setInterval(function(){ }, milliseconds) ... our outside: setInterval(dog, milliseconds) function dog () { } ... example: setInterval(function() { document.write("dog<br>") }, 100)
9th Jul 2017, 9:30 AM
Maz
Maz - avatar
+ 7
@visph is it a bad day? Noticed you're rude with some users today. I appreciate honesty, but maybe you don't think that some beginners can be afraid to write because of users like you. No need to be rude, just share your opinion, i learnt a lot from your comments and you inspired me in some works, i'm pretty sure you can be friendly or at least don't think about other people statements and share your knowledges in the respect of all. @Coder #1 if you want to customize the alert check out my code: https://code.sololearn.com/WguJrVB7EUMk/?ref=app
9th Jul 2017, 10:25 AM
Maz
Maz - avatar
+ 6
I got it, Calvin's answer was not the best to study but there was really need to write: "... Where is the code to study?" No need to discuss further, hope you got my point... the "goal to reach" was general, not just this single answer, otherwise i wouldn't have write here.
9th Jul 2017, 10:58 AM
Maz
Maz - avatar
+ 4
Sorry @Coder #1: I've placed respect into the fact to give you some advice... But there was nothing personal, just an opinion about the code, not at all about you ^^ (even clever people can do stupid things, myself first ;P)
9th Jul 2017, 12:12 PM
visph
visph - avatar
+ 3
setInterval() method (and setTimeout) can get more than the 2 first mandatory arguments: if given, the additional arguments will be passed to the callback function ;) function timed_func(a,b) { console.log('time_function('+a+','+b+')'); } window.setTimeout(timed_func,1000,'answer',42); This could be very helpfull in certain circumstances, as asynchronious call of function trough setInterval will lose context (ie. 'this' will be set with the 'window' object reference, even if you call it with an object method as callback ^^)
9th Jul 2017, 10:01 AM
visph
visph - avatar
+ 3
@Coder #1: Not original, stupid and bad written code: for(x=0;x<=100;x++){ setInterval(aler,1000); function aler(){ alert("spam!!!!!") } } + No need to loop, as setInterval will call the function in an implicit loop (call happen with delay between each) + you're defining your aler() custom function inside loop (making it redefining many times) + you don't have need of a custom function as you can pass argument to your callback function Fixed and simplified code, ever stupid and not original: window.setInterval(alert,0,"spam!!!!!"); ... that's all: it's enough to unfriendly block the user, without let him time to stop the script :P Anyway, you're asking in your code comments why alert box isn't customizable: alert, confirm and prompt boxes are done by operating system (rather by browser) as modal window (blocking/suspending execution of code and access to the page content). But however, if you want/need custom styled ones, you must implement some by yourself with Html/Css/JS (or use some framework/library ^^)
9th Jul 2017, 10:17 AM
visph
visph - avatar
+ 3
@Maz: I don't want to be rude, and if I'm little angry (and that should be sensitive in my previous post) it's essentially against me, to have not verify the JS code before running it ^^ I confess that this kind of unnecessary reboot of app and/or device make me angry... but I've tried to be constructive so I wrote a just explaining answer about it... giving the point to improve for teaching purpose, even if the goal to reach was... stupid: sorry for the word, but I cannot qualified it otherwise :P
9th Jul 2017, 10:44 AM
visph
visph - avatar
+ 2
It is setInterval.... I will say in something complex then >:) setInterval syntax; setInterval(<function>,<Repeated time in millseconds>) So how clearInterval work?: clearInterval(<number>) Absolutely setInterval return number which is the id of that interval (that is why we should paste setInterval inside variable) Confuse right?? >:)
9th Jul 2017, 9:32 AM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 2
About @Calvin answer, I was disapointing/surprising that he doesn't provide a better answer... Maybe todays isn't a day where I find good words to express myself ;P
9th Jul 2017, 11:04 AM
visph
visph - avatar
+ 2
Thx
9th Jul 2017, 1:16 PM
Ishaq Za'rour
9th Jul 2017, 10:04 AM
Ishaq Za'rour
+ 1
@vishp you could've said it in a more respectful way
9th Jul 2017, 11:18 AM
Ishaq Za'rour
0
Thx Guyz this was very helpful
9th Jul 2017, 10:03 AM
Ishaq Za'rour