Slideshow / Clearing All Javascript Timers
Ben Kittrell
05/31/2007 07:07PM
I decided to whip up a slideshow for the photo albums, since a few people have asked for it. First I thought about finding a flash one, but I knew that it would probably ask for a simple directory of images, and doodlekit doesn't store images that way. So I figured I could just use Ajax and some Scriptaculous effects instead. The end result is pretty slick
One thing I had to do to make it look so good is preload the next image, so you can't see it loading when the last one is fading. To do this I have a temporary div that sits over the main image div. I copy image A into the temp div, load image B in the main div, then fade the temp div. This requires me to use four different timers for various purposes to make everything look smooth.
However, I ran into a problem when I added the previous and next links. When you clicked on either, it loaded the proper image fine but the timers where still running. This started a chain reaction of all kinds of crap.
I needed a way to stop the timers, but I was using Rail's Javascript helpers which meant I couldn't just get the timer's id and call clearTimeout.
Instead I came up with a cool way to clear all the timers in one swoop. Basically I override the setTimeout method and store each timer id in an array. Then when I need to I just loop through the array and clear them all.
timers = new Array(); oldSetTimeout = window.setTimeout; window.setTimeout = function(code, interval) { timers.push(oldSetTimeout(code, interval)); } function resetTimeouts() { timers = new Array(); } function clearTimeouts() { for (var i= 0;i < timers.length; i++) { clearTimeout(timers[i]); } resetTimeouts(); }
I know this isn't totally elegant, but I think it's a good solution to a sticky problem. To use, just include this code and call clearTimeouts whenever you want. I extracted resetTimeouts so it could be called after each slide transition, and so the timer array didn't just keep growing.
Comments
Slideshow feature is awesome :)
Can't wait to see what you make next.
pretty slick ben. =)
Hey, it would also be pretty cool if the next, previous links also used the fade effect.
Yeah, I wanted to, but the timing is already pretty complicated and volatile. I might try that later.
Awesome little function, nice work!
Have a nice day for you!
Awesome little function, nice work!
we like<a href="http://www.swarovski-crystal-jewelry.net"><strong>swarovski jewellery< /strong></a>
and I always buy <a href="http://www.swarovski-crystal-jewelry.net"><strong>swarovski jewelry< /strong></a>
they have good quality <a href="http://www.swarovski-crystal-jewelry.net/swarovski-necklace-c-5.html"><strong>swarovski necklace/strong></a>
It's good javascript. I really appreciate the kind of topics you post here. Thanks for sharing us a great information that is actually helpful. Good day!
Those who know better than the good of those who, not the music, Good question, is good. And if they do not want to, just casually ask, that is able to get the correct answer,
Quite an excellent page you've shared in here. Impressive in here indeed. thanks for sharing.. http://www.goldceo.com/NeverWinter/NeverWinter-gold.shtml
Very good points you wrote here..Great stuffI think youve made some truly interesting points.Keep up the good work http://www.wtssb.com/gold.html
Good!This post is creative,you'll find a lot of new idea,it gives me inspiration.I believe I will also inspired by you and feel about extra new ideas.thanks http://shaiyagoldsell.com/gold.html
Everyday is an interesting new life adventure. Impressive indeed. thanks http://www.finshoes.com/
Very interesting discussion glad that I came across such informative post. Keep up the good work friend. Glad to be part of your net community. http://youtu.be/NzhWv7IL-bk
Brillaint work with awesome events conducting. Happy to see your last event and appreciating audience.
http://www.phillywedding.org
Good!This post is creative,you'll find a lot of new idea,it gives me inspiration.I believe I will also inspired by you and feel about extra new ideas.thanks
http://buletinresep.blogspot.com/
Thank you for the hard work you have made in writing this post. In the future I am hopeful the same best work from you as well. Actually your creative writing abilities has motivated me
This is such a great post, and was thinking much the same myself. Another great update.
Interesting and amazing how your post is! It Is Useful and helpful for me That I like it very much, and I am looking forward to Hearing from your next..
http://www.nhbensautobody.com
This is very interesting news website running 24 hours a day to keep their viewer updated all the time.
http://www.sddollhouse.com
This is very interesting news website running 24 hours a day to keep their viewer updated all the time. gemmes gratuit http://astucesaclashofclans.fr/astucesclashofclans.html
This is very interesting news website running 24 hours a day to keep their viewer updated all the time. Wholesale Gemstone online http://www.lonzejewelry.com
You’ve written nice post, I am gonna bookmark this page, thanks for info. I actually appreciate your own position and I will be sure to come back here.
http://www.robhuff.co.uk
I would like to thank you for the efforts you have made in writing this post.
http://www.mytzweb.co.uk
Thank you your page is very valuable for me. I really feel really delighted when found your page..!! new dubstep 2015 http://www.youtube.com/watch?v=XTK4gAkQPuk
I am really impressed with your work here today. I think you've highlighted each aspect of this topic clearly and I love that. I hope to see more of this same stuff! driving school http://www.intensivedrivingcourse.info/crashcourseblackpool.html
Thank you for sharing this interesting information .. I will keep visiting this web course with the latest information from you ..
http://indomotif.weebly.com/
http://situsmotor.blog.com/
Your blog! What should I say in its praise… relevant, lastly something which surely helped me? Thanks Chip Eng Seng Condo http://www.highparkresidences-condo.com
I don't be sorry that spent a few of minutes for reading. Write more often, surely’ll come to read amazing new.
I've been waiting for someone to share these articles. It really makes me think that I want to read more.On this subject, thank you very much to write such an interesting article.mty
http://www.camden-sc.com
Thank you. VENUS FACTOR REVIEWS http://thevenusfactorreviewsx.com/
Fantastic post however I was wanting to know if you could write a litte more on this subject? I'd be very thankful if you could elaborate a little bit more. Cheers!
http://www.resourcesworkforce.com
This is also a very good post which I really enjoyed reading. Thanks for the share and keep more post coming!
The website is looking bit flashy and it catches the visitors eyes. Design is pretty simple and a good user friendly interface. https://www.luckies.co.uk/gifts/fathers-day/
I acknowledge the same best work from you at some point or an alternate later.
Thank you for your post, I have found it here, this post gives me a lot of benefits.
Thank you for your post, I like your post and all that you share with us up to date and quite informative, I would like to bookmark a page so I can come here again to read your latest article. <a href="http://semaphone.weebly.com/ ">semaphone</a>
articles of interest .. I would always wait for other interesting articles from you .. thank you for sharing.
https://jipani.wordpress.com/
It's good javascript. I really appreciate the kind of topics you post here. Thanks for sharing us a great information that is actually helpful. Good day!
very interesting to learn .. thank you for sharing your article ..
Here is the worthy reading articles I have to recommend to you. You would earn much useful information like me.
All the contents you mentioned in post are too good and can be very useful. Thanks for sharing the information keep updating, looking forward for more posts.
Very efficiently written information. It will be priceless to anybody who uses it, together with myself. Sustain the good work for positive.
<a href="http://marmeren-werkbladen.nl">marmeren werkblad</a>
Very interesting points you have observed, appreciate it for putting up.
nice .. thanks for sharing
http://subaliadi.weebly.com/
Thank you for this interesting article, this post has great significance for me, it just makes me do things better life. success is always for you ..
http://arenagadget.heck.in/
http://baloktechno.blogspot.co.id/
This blog is great i love reading your posts. Keep up the great work! You know, a lot of people are hunting around for this info, you could help them greatly
valuable, and excellent design, as share good stuff with good ideas and concepts, lots of great information and inspiration, both of which I need, thanks to offering such a helpful information here.
You will be expected to take care of the daily cleaning of the yacht. This ranges from the guest’s bedrooms right up to the deck. So that the entire yacht is spotless and the decorations are polished,
Post a Comment