The Goal:
- Execute logic on each element in array.
- Wait
Xms in between next execution. mouseover(#slider)pauses delay – If delay = 1000ms, and 300ms had passed,mouseout(#slider)would trigger resume counting down the remaining 700ms delay.- After execution on last element, loop back to do it again – forever.
Here’s a visual explanation:
var = s Array(1,2,3)
var x = s[1]; //get first element
console.log(x); //do something to it
wait(); //START wait timer 1000ms
//------------> timer : 300ms
//------------> user : mouseover (#slider) : pause timer
//------------> user : waited 5000ms
//------------> user : mouseout (#slider) : resume timer
//------------> timer : 300ms --> still 700ms to go!
//------------> timer : 500ms
//------------> user : mouseover (#slider) : pause timer
//------------> user : waited 10000ms
//------------> user : mouseout (#slider) : resume timer
//------------> timer : 500ms --> still 500ms to go!
var x = s[2]; //get second element
console.log(x); //do something to it
wait(); //START wait timer 1000ms
//------------> timer : 200ms
//------------> user : mouseover (#slider) : pause timer
//------------> user : onclick (.slideButton1) : change index of array and clear timer
//------------> user : waited 6000ms
//------------> user : mouseout (#slider) : resume timer
//------------> timer : 0ms --> still 1000ms to go!
var x = s[1]; //get first element ( index was changed by clicking button )
console.log(x); //do something to it
wait(); //START wait timer 1000ms
// ... s[2] ... s[3] ...
//theres nothing else in the array, lets start back from s[1] again!
Solutions:
jQuery:
This solution came from a related post. The official source of this plugin can be found here.
Native JS:
This Answer by Aadit M Shah was really helpful. He also goes into detail about Delta Timing and how it can be useful in similar cases.
New Goal:
Abstract up either of these methods to allow use for other things.
Okay, I don’t use jquery and in all probability I have no clue as to what you’re trying to achieve. However from what I understand I think you should do something like this:
You will notice here that I’ve used a constructor called
DeltaTimer. This constructor is defined in this gist. It allows you to control your animations precisely usingstartandstopfunctions. Therenderfunction which is passed is given atimeargument which is aDate. The expressiontime - startgives the exact time when the function was called (e.g.4,1000,2000, …).The advantage of using
DeltaTimeroversetTimeoutorsetIntervalis:You can read my other answers regarding delta timing here, here and here.
Edit 1: That’s actually pretty simple. We simply shift out the first element of the array, process it and then push it back at the end. Here’s the logic:
Now we can create an array and loop through it as follows:
You can see the output here: http://jsfiddle.net/aGQfr/
Edit 2: Oops, I found a problem. From what I understand you want to process the next element a certain amount of time after the current element is finished processing. My delta timing script doesn’t do that. It only executes functions at fixed intervals of time.
So, you don’t need delta timing at all. You need to call
setTimeoutafter each element has been processed:After that just create an array and loop through it as follows:
You can see the demo here (note that your browser may not like it): http://jsfiddle.net/aGQfr/1/
Edit 3: You may also combined methods one and two so that you have a script which:
startandstopfunction.We’ll create a constructor function called
LoopIteratorwhich returns an iterator object withstartandstopmethods:Now we can create and start a new iterator as follows:
If you wish you may even stop and start the iterator when the mouse moves over an element or out of an element respectively:
When stopped the iterator’s state is preserved and when started again it continues from where it left off.
You may see the demo here: http://jsfiddle.net/PEcUG/
Edit 4: So you want to create a simple slider? Let’s start with the HTML, then the CSS and then the JavaScript.
The HTML:
We have a
divelement with a class calledslider(because there may be more than one slider on the page). Each slider has zero or moredivelements with the classslide. Each slide may have arbitrary content. The slider will also have buttons, but we don’t include this in the HTML as it will be generated automatically by JavaScript. No redundancy. Also note that none of the slides are manually numbered. Everything is handled by JavaScript.The CSS:
You may supply arbitrary CSS to suit your taste. An important point however is that
.slidemust havedisplay: none;because the slides must initially be hidden. Also.slider-buttonmust havefloat: right;. This is important as elements floated to the right have their order reversed. Thus the first button is actually the last button. This must be handled correctly by JavaScript so don’t change it unless you know what you’re doing.The JavaScript:
Alright, I’ll explain this bottom up:
Here
Slideris a constructor function which initializes and starts the slider element it passed. It accepts the time interval between two slides as the second argument. Here’s the code forSlider:The code is pretty self-explanatory. Every instance of
Sliderhas astart,stopandgotomethod for finer control. Thegotomethod takes a slide index number. Fornslides the indices range from0ton - 1. That’s it.The demo of the slider is here: http://jsfiddle.net/SyTFZ/4/