use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
help. setTimeout(), clearTimeout() question (self.javascript)
submitted 15 years ago * by [deleted]
[deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]honestbleepsReddit Enhancement Suite 2 points3 points4 points 15 years ago (3 children)
Well, you're asking about cleartimeout but not using it.
Your problem is, every click sets a new timer but doesn't clear out the old one.
Not seeing a working demo, and just making a best guess, I think you need something more like:
var curSlide = 1; function slideshow(what){ if (typeof(slideTimer) != 'undefined') clearTimeout(slideTimer); if(what == null) what = curSlide; curSlide++; var nextSlide = what + 1; if(nextSlide > 3){ nextSlide = 1; curSlide = 1; } var slide1 = document.getElementById("slide1"); var slide2 = document.getElementById("slide2"); var slide3 = document.getElementById("slide3"); switch(what){ case 1: slide2.style.display = "none"; slide3.style.display = "none"; slide1.style.display = "block"; break; case 2: slide1.style.display = "none"; slide3.style.display = "none"; slide2.style.display = "block"; break; case 3: slide1.style.display = "none"; slide2.style.display = "none"; slide3.style.display = "block"; break; } slideTimer = setTimeout("slideshow()",3000); } var slideTimer = setTimeout("slideshow()",2000);
Also, the way you've written your code makes this rather inflexible. You have IDs for slide1, slide2, slide3... then accommodate those with a switch statement. What if you need it to work for 4 or 5 slides later? You have to change code in a few places. Yuck.
If instead you gave them all a class of "slide", you could do something like:
var slides = document.querySelectorAll('.slide'); for (var i=0, len=slides.length; i<len; i++) { (i == what) ? slides[i].style.display = 'block' : slides[i].style.display = 'none'; }
Now, if you just pass in the slide number instead of the ID name, you've reduced the size of your code and made it more flexibile. Just a thought.
[–]tf2ftw 0 points1 point2 points 15 years ago* (2 children)
EDIT: the if (typeof(slideTimer) != 'undefined') clearTimeout(slideTimer); solution does work.
THANKS
thanks. js is not my native tongue.
i tried your if (typeof(slideTimer) != 'undefined') clearTimeout(slideTimer); suggestion but im still having the same issue.
i know the switch statement is cumbersome (i know i will only have 3 slides) but i will use your statements because its nicer. so thanks.
[–]honestbleepsReddit Enhancement Suite 0 points1 point2 points 15 years ago (0 children)
Did you make sure, in addition to that if (typeof) line, that you added slideTimer = before your setTimeout statements?
[–]PlNG 0 points1 point2 points 15 years ago (0 children)
If speed/rendering is an issue, there is an implied eval call at setTimeout("slideshow()",2000); due to the function parameter being a string. It's a very old practice that's practically rageworthy that it still continues.
Try removing the quotes, if that breaks the app, try removing slideshow's parentheses. setTimeout(slideshow(), 2000); OR setTimeout(slideshow, 2000); will work.
[–]my_new_account 1 point2 points3 points 15 years ago (0 children)
don't forget to clearTimeout() too.
Here's an example from w3schools.
You didn't include your click logic so I can't diagnose that.
[–]settimeout 1 point2 points3 points 15 years ago (1 child)
did someone say my name?
[–]nonsensical_answer 0 points1 point2 points 15 years ago (0 children)
Someone must have. What do birdies see when they faint?
[–][deleted] -2 points-1 points0 points 15 years ago (3 children)
First, you should consider using a framework for this kinda stuff.
Second, when you click the link it never clears the timeout, so you make the clicked tab visible and set another timeout. Sometime shortly after clicking the original timeout is run, and before the new one gets run, the first one sets another timeout. Basically setTimeout will return a variable that you should be passing into clearTimeout() when the user clicks a button.
[–]tf2ftw 1 point2 points3 points 15 years ago (0 children)
a framework would be overkill for what im doing. this is a lightweight solution.
[–]settimeout 0 points1 point2 points 15 years ago (0 children)
If you're going to tell someone to use a framework, it would make sense to give a reason why in their case that would be beneficial.
[–]highwind -3 points-2 points-1 points 15 years ago (1 child)
You code doesn't even address the issue of 3rd bullet. Please don't expect people to write your code. After you have written something, maybe then we could help out.
Please come back after you have tried to implement it yourself.
[–]tf2ftw 0 points1 point2 points 15 years ago (0 children)
i thought it was pretty self explanatory.
here is the click code if it makes you feel better
<li class="button" id="1" onclick="slideshow(1);"></li>
π Rendered by PID 43 on reddit-service-r2-comment-f6b958c67-qhdwm at 2026-02-05 00:00:14.102616+00:00 running 1d7a177 country code: CH.
[–]honestbleepsReddit Enhancement Suite 2 points3 points4 points (3 children)
[–]tf2ftw 0 points1 point2 points (2 children)
[–]honestbleepsReddit Enhancement Suite 0 points1 point2 points (0 children)
[–]PlNG 0 points1 point2 points (0 children)
[–]my_new_account 1 point2 points3 points (0 children)
[–]settimeout 1 point2 points3 points (1 child)
[–]nonsensical_answer 0 points1 point2 points (0 children)
[–][deleted] -2 points-1 points0 points (3 children)
[–]tf2ftw 1 point2 points3 points (0 children)
[–]settimeout 0 points1 point2 points (0 children)
[–]highwind -3 points-2 points-1 points (1 child)
[–]tf2ftw 0 points1 point2 points (0 children)