/    Sign up×
Community /Pin to ProfileBookmark

Adding a pause function to my gallery

Ok, so I have a very simple gallery right now. It has a next and previous working buttons, and I have it loop every couple seconds. The only problem I have is that when I click next or previous it doesn’t continue the loop after I hit next again.

Here are the two snippets of code.

[code]
$(‘#slideshow .arrow’).click(function(){
var li = slides.eq(current),
canvas = li.find(‘canvas’),
nextIndex = 0;

// Depending on whether this is the next or previous
// arrow, calculate the index of the next slide accordingly.

if($(this).hasClass(‘next’)){
nextIndex = current >= slides.length-1 ? 0 : current+1;
}
if($(this).hasClass(‘previous’)){
nextIndex = current <= 0 ? slides.length-1 : current-1;
}
[/code]

and

[code]

$(window).load(function(){

// The window.load event guarantees that
// all the images are loaded before the
// auto-advance begins.

var timeOut = null;

$(‘#slideshow .arrow’).click(function(e,simulated){

// The simulated parameter is set by the
// trigger method.

if(!simulated){

// A real click occured. Cancel the
// auto advance animation.

clearTimeout(timeOut);
}
});

(function autoAdvance(){

// Simulating a click on the next arrow.
$(‘#slideshow .next’).trigger(‘click’,[true]);

// Schedulling a time out in 5 seconds.
timeOut = setTimeout(autoAdvance,1000);

})();

});
[/code]

I’ve been trying to figure this out without posting on the forums, but I am stumped.

Thanks for the help

This is the tutorial I followed.

[url]http://tutorialzine.com/2010/09/html5-canvas-slideshow-jquery/[/url]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@nap0leonFeb 12.2012 — replace
clearTimeout(timeOut);

with a new value for the timeout

e.g.,
timeOut = setTimeout(autoAdvance,1000);

BTW, the comments in the code are incorrect... "1000" milliseconds is one second, not five.
Copy linkTweet thisAlerts:
@clandestauthorFeb 12.2012 — Thank you for your reply, but that didn't seem to work.

Here is my entire code

<i>
</i>$(window).load(function(){

<i> </i>// We are listening to the window.load event, so we can be sure
<i> </i>// that the images in the slideshow are loaded properly.

<i> </i>// Testing wether the current browser supports the canvas element:
<i> </i>var supportCanvas = 'getContext' in document.createElement('canvas');

<i> </i>// The canvas manipulations of the images are CPU intensive,
<i> </i>// this is why we are using setTimeout to make them asynchronous
<i> </i>// and improve the responsiveness of the page.

<i> </i>var slides = $('#slideshow li'),
<i> </i> current = 0,
<i> </i> slideshow = {width:0,height:0};

<i> </i>var timeOut = null;

<i> </i>$('#slideshow .arrow').click(function(e,simulated){

<i> </i> // The simulated parameter is set by the
<i> </i> // trigger method.

<i> </i> if(!simulated){

<i> </i> // A real click occured. Cancel the
<i> </i> // auto advance animation.

<i> </i> clearTimeout(timeOut);
<i> </i> }
<i> </i>});

<i> </i>(function autoAdvance(){

<i> </i> // Simulating a click on the next arrow.
<i> </i> $('#slideshow .next').trigger('click',[true]);

<i> </i> // Schedulling a time out in 5 seconds.
<i> </i> timeOut = setTimeout(autoAdvance,1000);
<i> </i>})();

<i> </i>setTimeout(function(){

<i> </i> if(supportCanvas){
<i> </i> $('#slideshow img').each(function(){

<i> </i> if(!slideshow.width){
<i> </i> // Saving the dimensions of the first image:
<i> </i> slideshow.width = this.width;
<i> </i> slideshow.height = this.height;
<i> </i> }

<i> </i> // Rendering the modified versions of the images:
<i> </i> createCanvasOverlay(this);
<i> </i> });
<i> </i> }

<i> </i> $('#slideshow .arrow').click(function(){
<i> </i> var li = slides.eq(current),
<i> </i> canvas = li.find('canvas'),
<i> </i> nextIndex = 0;

<i> </i> // Depending on whether this is the next or previous
<i> </i> // arrow, calculate the index of the next slide accordingly.

<i> </i> if($(this).hasClass('next')){
<i> </i> nextIndex = current &gt;= slides.length-1 ? 0 : current+1;
<i> </i> }
<i> </i> if($(this).hasClass('previous')){
<i> </i> nextIndex = current &lt;= 0 ? slides.length-1 : current-1;
<i> </i> }


<i> </i> var next = slides.eq(nextIndex);

<i> </i> if(supportCanvas){

<i> </i> // This browser supports canvas, fade it into view:

<i> </i> canvas.fadeIn(function(){

<i> </i> // Show the next slide below the current one:
<i> </i> next.show();
<i> </i> current = nextIndex;

<i> </i> // Fade the current slide out of view:
<i> </i> li.fadeOut(function(){
<i> </i> li.removeClass('slideActive');
<i> </i> canvas.hide();
<i> </i> next.addClass('slideActive');
<i> </i> });
<i> </i> });
<i> </i> }
<i> </i> else {

<i> </i> // This browser does not support canvas.
<i> </i> // Use the plain version of the slideshow.

<i> </i> current=nextIndex;
<i> </i> next.addClass('slideActive').show();
<i> </i> li.removeClass('slideActive').hide();
<i> </i> }
<i> </i> });

<i> </i>},100);
<i> </i>// This function takes an image and renders
<i> </i>// a version of it similar to the Overlay blending
<i> </i>// mode in Photoshop.

<i> </i>function createCanvasOverlay(image){

<i> </i> var canvas = document.createElement('canvas'),
<i> </i> canvasContext = canvas.getContext("2d");

<i> </i> // Make it the same size as the image
<i> </i> canvas.width = slideshow.width;
<i> </i> canvas.height = slideshow.height;

<i> </i> // Drawing the default version of the image on the canvas:
<i> </i> canvasContext.drawImage(image,0,0);

<i> </i> // Taking the image data and storing it in the imageData array:
<i> </i> var imageData = canvasContext.getImageData(0,0,canvas.width,canvas.height),
<i> </i> data = imageData.data;

<i> </i> // Loop through all the pixels in the imageData array, and modify
<i> </i> // the red, green, and blue color values.
<i> </i>/*****************************
<i> </i> for(var i = 0,z=data.length;i&lt;z;i++){

<i> </i> // The values for red, green and blue are consecutive elements
<i> </i> // in the imageData array. We modify the three of them at once:

<i> </i> data[i] = ((data[i] &lt; 128) ? (2*data[i]*data[i] / 255) :
<i> </i> (255 - 2 * (255 - data[i]) * (255 - data[i]) / 255));
<i> </i> data[++i] = ((data[i] &lt; 128) ? (2*data[i]*data[i] / 255) :
<i> </i> (255 - 2 * (255 - data[i]) * (255 - data[i]) / 255));
<i> </i> data[++i] = ((data[i] &lt; 128) ? (2*data[i]*data[i] / 255) :
<i> </i> (255 - 2 * (255 - data[i]) * (255 - data[i]) / 255));

<i> </i> // After the RGB channels comes the alpha value, which we leave the same.
<i> </i> ++i;
<i> </i> }
<i> </i>***************************/
<i> </i> // Putting the modified imageData back on the canvas.
<i> </i> canvasContext.putImageData(imageData,0,0,0,0,imageData.width,imageData.height);

<i> </i> // Inserting the canvas in the DOM, before the image:
<i> </i> image.parentNode.insertBefore(canvas,image);
<i> </i>}

});


and the comments are wrong, because I am changing the code from the original tutorial that I was following to get what I need ?

can I not have something like this?

<i>
</i>$('#slideshow .arrow').click(function(){
var li = slides.eq(current),
canvas = li.find('canvas'),
nextIndex = 0;

<i> </i> // Depending on whether this is the next or previous
<i> </i> // arrow, calculate the index of the next slide accordingly.

<i> </i> if($(this).hasClass('next')){
<i> </i> nextIndex = current &gt;= slides.length-1 ? 0 : current+1;
//this just resets timeOut, or atleast I think it should when clicking next.
timeOut = setTimeout(autoAdvance,1000);

<i> </i> }
<i> </i> if($(this).hasClass('previous')){
<i> </i> nextIndex = current &lt;= 0 ? slides.length-1 : current-1;
<i> </i> }
Copy linkTweet thisAlerts:
@nap0leonFeb 12.2012 — By "doesn't work", do you mean that it now continues without the timer resetting or it does not autodvance at all anymore?

According to the comments, this is the code that cancels the autoAdvance
<i>
</i> // The simulated parameter is set by the
// trigger method.

<i> </i> if(!simulated){

<i> </i> // A real click occured. Cancel the
<i> </i> // auto advance animation.

<i> </i> clearTimeout(timeOut);
<i> </i> }


  • - To stop it from stopping, just remove the "clearTimeout" line.

  • - To start the delay over again, you should just need to reset the timer using "timeOut = setTimeout(autoAdvance,1000);"... you can try leaving the clearTimeout line in and then setting the new timeOut.
    <i>
    </i>clearTimeout(timeOut);
    timeOut = setTimeout(autoAdvance,1000);
  • Copy linkTweet thisAlerts:
    @nap0leonFeb 12.2012 — If you can post an example HTML page with only the useful pieces (including link to the jQuery library you are using and the required HTML elements that demonstrate the images used for the slideshow), I'll fiddle with it (using images on my local machine... no need to post images or anything like that).
    Copy linkTweet thisAlerts:
    @clandestauthorFeb 12.2012 — by doesn't work I mean it doesn't auto advance,


    and here is the code of the html page.
    <i>
    </i>&lt;div id="slideshow"&gt;
    &lt;ul class="slides"&gt;
    &lt;li&gt;&lt;img src="images/portfolio/people/01.jpg" width="533" height="800"&gt;&lt;/li&gt;
    &lt;li&gt;&lt;img src="images/portfolio/people/02.jpg" width="531" height="800"&gt;&lt;/li&gt;
    &lt;li&gt;&lt;img src="images/portfolio/people/03.jpg" width="571" height="800"&gt;&lt;/li&gt;
    &lt;li&gt;&lt;img src="images/portfolio/people/04.jpg" width="573" height="800"&gt;&lt;/li&gt;
    &lt;li&gt;&lt;img src="images/portfolio/people/05.jpg" width="800" height="533"&gt;&lt;/li&gt;
    &lt;li&gt;&lt;img src="images/portfolio/people/06.jpg" width="800" height="533"&gt;&lt;/li&gt;
    &lt;li&gt;&lt;img src="images/portfolio/people/07.jpg" width="721" height="800"&gt;&lt;/li&gt;
    &lt;li&gt;&lt;img src="images/portfolio/people/08.jpg" width="664" height="800"&gt;&lt;/li&gt;
    &lt;li&gt;&lt;img src="images/portfolio/people/09.jpg" width="533" height="800"&gt;&lt;/li&gt;
    &lt;li&gt;&lt;img src="images/portfolio/people/10.jpg" width="587" height="800"&gt;&lt;/li&gt;
    &lt;li&gt;&lt;img src="images/portfolio/people/11.jpg" width="800" height="557"&gt;&lt;/li&gt;
    &lt;li&gt;&lt;img src="images/portfolio/people/12.jpg" width="533" height="800"&gt;&lt;/li&gt;
    &lt;li&gt;&lt;img src="images/portfolio/people/13.jpg" width="518" height="800"&gt;&lt;/li&gt;
    &lt;/ul&gt;
    &lt;span class="arrow previous"&gt;&lt;/span&gt;
    &lt;span class="arrow pause"&gt;&lt;/span&gt;
    &lt;span class="arrow next"&gt;&lt;/span&gt;
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;div id="footer"&gt;

    <i> </i> &lt;/div&gt;
    <i> </i> &lt;/div&gt;
    <i> </i>&lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
    <i> </i>&lt;script src="script.js"&gt;&lt;/script&gt;
    <i> </i>&lt;script src="autoadvance.js"&gt;&lt;/script&gt;
    <i> </i>&lt;/body&gt;
    &lt;/html&gt;


    and the full html code
    <i>
    </i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"&gt;
    &lt;html&gt;
    &lt;head&gt;
    &lt;title&gt;Brandon Scales - People&lt;/title&gt;
    &lt;meta name="" content=""&gt;
    &lt;link rel="stylesheet" type="text/css" href="style.css" /&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;div id="container"&gt;
    &lt;div id="header"&gt;
    &lt;h1&gt;Brandon Scales&lt;/h1&gt;
    &lt;/div&gt;
    &lt;div id="nav"&gt;
    &lt;ul&gt;
    &lt;li&gt;&lt;b&gt;Portfolio&lt;/b&gt;
    &lt;ul&gt;
    &lt;li&gt;&lt;a href="people.php"&gt;People&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="places.php"&gt;Places&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="street.php"&gt;Street&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="personal.php"&gt;Personal&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
    &lt;/li&gt;
    &lt;li&gt;
    &lt;b&gt;Projects&lt;/b&gt;
    &lt;ul&gt;
    &lt;li&gt;&lt;a href="gordon_ramsey.php"&gt;Gordon Ramsey&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="two_rabbits.php"&gt;Two Rabbits&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://www.cell-out.tumblr.com/" target="_blank"&gt;*Cell-Out&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
    &lt;/li&gt;
    &lt;/ul&gt;
    &lt;/div&gt;
    &lt;div id="body"&gt;
    &lt;div id="slideshow"&gt;
    &lt;ul class="slides"&gt;
    &lt;li&gt;&lt;img src="images/portfolio/people/01.jpg" width="533" height="800"&gt;&lt;/li&gt;
    &lt;li&gt;&lt;img src="images/portfolio/people/02.jpg" width="531" height="800"&gt;&lt;/li&gt;
    &lt;li&gt;&lt;img src="images/portfolio/people/03.jpg" width="571" height="800"&gt;&lt;/li&gt;
    &lt;li&gt;&lt;img src="images/portfolio/people/04.jpg" width="573" height="800"&gt;&lt;/li&gt;
    &lt;li&gt;&lt;img src="images/portfolio/people/05.jpg" width="800" height="533"&gt;&lt;/li&gt;
    &lt;li&gt;&lt;img src="images/portfolio/people/06.jpg" width="800" height="533"&gt;&lt;/li&gt;
    &lt;li&gt;&lt;img src="images/portfolio/people/07.jpg" width="721" height="800"&gt;&lt;/li&gt;
    &lt;li&gt;&lt;img src="images/portfolio/people/08.jpg" width="664" height="800"&gt;&lt;/li&gt;
    &lt;li&gt;&lt;img src="images/portfolio/people/09.jpg" width="533" height="800"&gt;&lt;/li&gt;
    &lt;li&gt;&lt;img src="images/portfolio/people/10.jpg" width="587" height="800"&gt;&lt;/li&gt;
    &lt;li&gt;&lt;img src="images/portfolio/people/11.jpg" width="800" height="557"&gt;&lt;/li&gt;
    &lt;li&gt;&lt;img src="images/portfolio/people/12.jpg" width="533" height="800"&gt;&lt;/li&gt;
    &lt;li&gt;&lt;img src="images/portfolio/people/13.jpg" width="518" height="800"&gt;&lt;/li&gt;
    &lt;/ul&gt;
    &lt;span class="arrow previous"&gt;&lt;/span&gt;
    &lt;span class="arrow pause"&gt;&lt;/span&gt;
    &lt;span class="arrow next"&gt;&lt;/span&gt;
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;div id="footer"&gt;

    <i> </i> &lt;/div&gt;
    <i> </i> &lt;/div&gt;
    <i> </i>&lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
    <i> </i>&lt;script src="script.js"&gt;&lt;/script&gt;
    <i> </i>&lt;script src="autoadvance.js"&gt;&lt;/script&gt;
    <i> </i>&lt;/body&gt;
    &lt;/html&gt;


    thanks a lot! appreciate it.
    Copy linkTweet thisAlerts:
    @nap0leonFeb 13.2012 — Alas, I'm stumped.

    Maybe the site you got the code from can help?

    So sorry...
    ×

    Success!

    Help @clandest spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.27,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...