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: - function allPlantPick(){
-
-
setInterval("plantPick('plant1')", plantTimer());
-
-
}
-
-
function plantTimer() {
-
-
var plantNum = (Math.random()*4000) + 1000;
-
return(plantNum);
-
-
}
-
-
//this function works fine:
-
function plantPick(myFoo){
-
-
var plantChoices=["images/p1.jpg","images/p2.jpg","images/p3.jpg"];
-
-
document.getElementById(myFoo).style.backgroundImage = "url(" + plantChoices[ Math.floor(Math.random()*plantChoices.length) ] + ")";
-
}
6 5575 pbmods 5,821
Recognized Expert Expert
Heya, Steve.
Instead of setInterval(), use setTimeout(): -
function allPlantPick(){
-
-
setTimeout(
-
function()
-
{
-
plantPick('plant1');
-
allPlantPick();
-
},
-
plantTimer()
-
);
-
}
-
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: - function allPlantPick(){
-
setTimeout(
-
function()
-
{
-
plantPick('plant1');
-
allPlantPick();
-
},
-
plantTimer()
-
);
-
setTimeout(
-
function()
-
{
-
plantPick('plant2');
-
allPlantPick();
-
},
-
plantTimer()
-
);
-
setTimeout(
-
function()
-
{
-
plantPick('plant3');
-
allPlantPick();
-
},
-
plantTimer()
-
);
-
}
-
-
function plantTimer() {
-
-
var plantNum = (Math.random()*4000) + 1000;
-
-
return(plantNum);
-
-
}
-
-
//this function works fine:
-
-
function plantPick(myFoo){
-
-
var plantChoices=["images/p1.jpg","images/p2.jpg","images/p3.jpg"];
-
-
document.getElementById(myFoo).style.backgroundIma ge = "url(" + plantChoices[ Math.floor(Math.random()*plantChoices.length) ] + ")";
-
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
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: -
function allPlantPick(plantId)
-
{
-
setTimeout(
-
function()
-
{
-
plantPick(plantId);
-
allPlantPick(plantId);
-
},
-
plantTimer()
-
);
-
}
-
-
for(var i = 1; i < 4; ++i)
-
{
-
allPlantPick('plant' + i);
-
}
-
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: - var q = 1;
-
-
function pStopGo() {
-
-
if (q == 1) {
-
stopPlant();
-
q = 2;
-
}
-
else if (q == 2) {
-
goPlant();
-
q = 1;
-
}
-
-
function stopPlant(){
-
window.clearTimeout(startStop);
-
}
-
-
function goPlant(){
-
allPlantPick();
-
}
-
-
}
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: -
var activeTransitions = {};
-
-
function allPlantPick(plantId)
-
{
-
activeTransitions[plantId] = setTimeout(
-
function()
-
{
-
plantPick(plantId);
-
allPlantPick(plantId);
-
},
-
plantTimer()
-
);
-
}
-
So to stop a particular timeout from firing: -
clearTimeout(activeTransitions['plant2'];
-
Or to stop all three: -
for(plantid in activeTransitions)
-
{
-
clearTimeout(activeTransitions[plantid];
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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++?
|
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.
|
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
|
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
| |
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
|
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...
|
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.
|
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..
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |