473,811 Members | 2,911 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

setting a random time interval for a slideshow?

12 New Member
hi,

this should be simple but its stumping me,

I am trying to make a slideshow that pulls random images at a random time interval (between 1 and 4 seconds). The images part works fine, and I can get the time function to create a random number, but that number doesnt change throughout the duration of the slideshow. I would like the duration to reset itself and changefor every picture that is displayed. Here is my code:

Expand|Select|Wrap|Line Numbers
  1. function allPlantPick(){
  2.  
  3.     setInterval("plantPick('plant1')", plantTimer());
  4.  
  5. }
  6.  
  7. function plantTimer() {
  8.  
  9.     var plantNum = (Math.random()*4000) + 1000;
  10.     return(plantNum);
  11.  
  12. }
  13.  
  14. //this function works fine:
  15. function plantPick(myFoo){
  16.  
  17.  var plantChoices=["images/p1.jpg","images/p2.jpg","images/p3.jpg"];
  18.  
  19.  document.getElementById(myFoo).style.backgroundImage = "url(" + plantChoices[ Math.floor(Math.random()*plantChoices.length) ] + ")";
  20. }
Aug 14 '07 #1
6 5575
pbmods
5,821 Recognized Expert Expert
Heya, Steve.

Instead of setInterval(), use setTimeout():
Expand|Select|Wrap|Line Numbers
  1. function allPlantPick(){
  2.  
  3.     setTimeout(
  4.         function()
  5.         {
  6.             plantPick('plant1');
  7.             allPlantPick();
  8.         },
  9.         plantTimer()
  10.     );
  11. }
  12.  
Aug 14 '07 #2
newsteve1
12 New Member
Hey thanks for the prompt reply,

It worked perfect. However, I forgot to mention that I want to have this random slideshow functioning simultaneously in 3 separate divs that are side by side. Each div will have random pictures and random timing. I naturally just multiplied your code by 3 and stuck it all into allPlantPick, but when I tested it, it created this crazy situation where the random timing kept speeding up until it crashed firefox! It was cool but not what I wanted. Should I be calling 3 separate functions instead of just lumping them into one??? Heres what I did:


Expand|Select|Wrap|Line Numbers
  1. function allPlantPick(){
  2.           setTimeout(
  3.               function()
  4.               {
  5.                   plantPick('plant1');
  6.                   allPlantPick();
  7.               },
  8.               plantTimer()
  9.           );
  10.           setTimeout(
  11.               function()
  12.               {
  13.                   plantPick('plant2');
  14.                   allPlantPick();
  15.               },
  16.               plantTimer()
  17.           );
  18.           setTimeout(
  19.               function()
  20.               {
  21.                   plantPick('plant3');
  22.                   allPlantPick();
  23.               },
  24.               plantTimer()
  25.           );
  26.       }
  27.  
  28. function plantTimer() {
  29.  
  30.     var plantNum = (Math.random()*4000) + 1000;
  31.  
  32.     return(plantNum);
  33.  
  34. }
  35.  
  36. //this function works fine:
  37.  
  38. function plantPick(myFoo){
  39.  
  40.  var plantChoices=["images/p1.jpg","images/p2.jpg","images/p3.jpg"];
  41.  
  42.  document.getElementById(myFoo).style.backgroundIma  ge = "url(" + plantChoices[ Math.floor(Math.random()*plantChoices.length) ] + ")";
  43.  
Aug 14 '07 #3
newsteve1
12 New Member
No worries, I just put it in three separate functions and called those three separate functions and it all worked out... i am curious as to why the slide show kept speeding up though...?

Thanks again for your help,

Steve
Aug 14 '07 #4
pbmods
5,821 Recognized Expert Expert
Heya, Steve.

The reason why the timer keeps speeding up is because allPlantPick() sets up 3 timers... and *each* of those timers calls allPlantPick(). .. which sets up *3* more timers!

Perhaps you can see how that would be a problem... :P

Instead, consider doing this:
Expand|Select|Wrap|Line Numbers
  1. function allPlantPick(plantId)
  2. {
  3.     setTimeout(
  4.               function()
  5.               {
  6.                   plantPick(plantId);
  7.                   allPlantPick(plantId);
  8.               },
  9.               plantTimer()
  10.           );
  11. }
  12.  
  13. for(var i = 1; i < 4; ++i)
  14. {
  15.     allPlantPick('plant' + i);
  16. }
  17.  
Aug 14 '07 #5
newsteve1
12 New Member
ok, i get it now. and your script works totally.
not that you havent helped me out enough already, but im looking to put a user controlled stop/start option with an onclick applied to each image...

this is what i have so far... i was thinking i would throw the entire set interval into a variable (startStop), but again its tricky with 3 separate slideshows:

Expand|Select|Wrap|Line Numbers
  1. var q = 1;
  2.  
  3. function pStopGo() {
  4.  
  5.     if (q == 1) {
  6.         stopPlant();
  7.         q = 2;
  8.     }
  9.     else if (q == 2) {
  10.         goPlant();
  11.         q = 1;
  12.     }
  13.  
  14. function stopPlant(){
  15.     window.clearTimeout(startStop);
  16. }
  17.  
  18. function goPlant(){
  19.     allPlantPick();
  20. }
  21.  
Aug 14 '07 #6
pbmods
5,821 Recognized Expert Expert
Heya, Steve.

Given that you will always have three separate timeouts going at the same time, you can store each one inside of a global variable:

Expand|Select|Wrap|Line Numbers
  1. var activeTransitions = {};
  2.  
  3. function allPlantPick(plantId)
  4. {
  5.     activeTransitions[plantId] = setTimeout(
  6.               function()
  7.               {
  8.                   plantPick(plantId);
  9.                   allPlantPick(plantId);
  10.               },
  11.               plantTimer()
  12.           );
  13. }
  14.  
So to stop a particular timeout from firing:
Expand|Select|Wrap|Line Numbers
  1. clearTimeout(activeTransitions['plant2'];
  2.  
Or to stop all three:
Expand|Select|Wrap|Line Numbers
  1. for(plantid in activeTransitions)
  2. {
  3.     clearTimeout(activeTransitions[plantid];
  4. }
  5.  
Aug 14 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
4995
by: christian | last post by:
Hi, i'm getting no success, with my attempt displaying some pictures one after another for i.e. some seconds. null.jpg is a white site! Really thanks for any help, regards,Christian
16
4230
by: Jason | last post by:
Hi, I need a way to use random numbers in c++. In my c++ project, when using the mingw compiler I used a mersenne twister that is publicly available and this did its job well. Now I have shelled out on VC++ 6.0 compiling that same code is proving difficult. I am not too worried how I generate random numbers in c++, as long as it is sufficiently random. Can anybody help me out in getting what I want from VC++?
14
2564
by: Crirus | last post by:
This is more a logical problem than a VB. I have this situation: A timer that need to tick at each 10 minutes starting on minute 15 of curent hour. But I want to calculate the next tick time whenever I want to start the timer.
2
1984
by: mikeoley | last post by:
Ok I have a Javascript slideshow working. Every image is linked to a another page in the site. Problem is...The link wont refresh to the next link once the second images rollovers in the slideshow. It only stays at the first images link. Is this a cache issue? Or is there anyway to create a random number to trick this or make it work properly. I'm very raw with Javascript so I'm having trouble figuring this out. Thank you in advance
1
8048
by: mynameisnotbob | last post by:
How could I make a random image slideshow with the images being in a folder and not listed in the head tag? (I have over 1,000 images and do not want to list them all in my index page) ? Thanks
15
2547
by: Papajo | last post by:
Hi, This script will write a random number into a document write tag, I've been trying to get it to write into a input form box outside the javascript, any help is appreciated. Thanks Joe http://web2jo.com/Work/Random_Temp.html
8
2001
by: kp87 | last post by:
I am a little bit stuck .... I want to play a bunch of soundfiles randomly, but i want to give each soundfile a rating (say 0-100) and have the likelihood that the file be chosen be tied to its rating so that the higher the rating the more likely a file is to be chosen. Then i need some additional flags for repetition, and some other business. I am guessing a dictionary would be a great way to do this, with the key being the soundfile...
104
5202
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a better way than to fill an array of range 0... RAND_MAX with pre-computed primes and using the output of rand() to index into it to extract a random prime.
7
6098
by: moni | last post by:
Hi, Can anyone tell me , how I would generate random numbers, for an interval say from 15 to 450 in C. Plz lemme know. thanx..
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10651
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10392
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10136
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7671
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6893
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5693
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3020
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.