Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7743699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:36:07+00:00 2026-06-01T09:36:07+00:00

I have the following code: // After 8 seconds change the slide… var timeout

  • 0

I have the following code:

// After 8 seconds change the slide...
        var timeout = setTimeout("changeSlide()", 8000);

        $('div.slideshow').mouseover(function() {

            // If the user hovers the slideshow then reset the setTimeout
            clearTimeout(timeout);
        });

        $('div.slideshow').mouseleave(function() {

            clearTimeout(timeout);
            var timeout = setTimeout("changeSlide()", 8000);

        });

What I want to happen is make the function changeSlide run EVERY 8 seconds in a loop unless someone hovers the slideshow div. When they remove the cursor then do the timeout again!

However the loop only happens once and the hover doesn’t stop the timeout or start it again :/

EDIT:

This loops great but the hover on and off causes the function to run multiple times:

// After 8 seconds change the slide...
        var timeout = setInterval(changeSlide, 2000);

        $('div.slide').mouseover(function() {

            // If the user hovers the slideshow then reset the setTimeout
            clearInterval(timeout);
        });

        $('div.slide').mouseleave(function() {

            clearInterval(timeout);
            var timeout = setInterval(changeSlide, 2000);

        });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T09:36:08+00:00Added an answer on June 1, 2026 at 9:36 am

    You have a couple issues here. First off, when you set a timeout, you need to store the return of that function call into a variable if you potentially want to stop it.

        var slide_timer = setTimeout(changeSlide, 8000);
    

    Second, when you call clearTimeout (rather than clearInterval), you need to pass it an argument. What argument? That variable you stored when you called setTimeout

            clearTimeout(slide_timer);
    

    Third, when you use setTimeout, it only fires once. setInterval will continue to fire, then you’d use clearInterval to stop it.

    There is an issue in timing with using intervals rather than timeouts. The browser treats them subtly differently, and it may be important to your code to know the difference and use the proper method. If you use intervals, since they only fire once, you’ll have to re-establish the timeout every time it fires.

    var slide_timer = setTimeout(function () {
        changeSlide();
        var slide_timer = setTimeout(changeSlide, 8000);
    }, 8000);
    

    OR

    var slide_timer = setTimeout(changeSlide, 8000);
    ...
    function changeSlide() {
       ... your code ...
        var slide_timer = setTimeout(changeSlide, 8000);
    }
    

    (I prefer the former method)

    And lastly, whether you use timeouts or intervals, don’t pass a string to setTimeout, pass a function reference. See the sample code above, or like this:

    var slide_timer = setTimeout("changeSlide()", 8000); // <--- DON'T
    var slide_timer = setTimeout(changeSlide, 8000);     // <--- DO
    var slide_timer = setTimeout(function () {           // <--- DO
        changeSlide() ;
        // other script
    }, 8000); 
    

    Putting it all together:

    // After 8 seconds change the slide...
        var slide_timer = setTimeout(changeSlide, 8000);
        $('div.slideshow').hover(function() {
            // If the user hovers the slideshow then reset the setTimeout
            clearTimeout(slide_timer);
        }, function() {
            slide_timer = setInterval(changeSlide, 8000);
        });
    

    Documentation

    • clearTimeout – https://developer.mozilla.org/en/DOM/window.clearTimeout
    • setTimeout – https://developer.mozilla.org/en/DOM/window.setTimeout
    • clearInterval – https://developer.mozilla.org/en/DOM/window.clearInterval
    • setInterval – https://developer.mozilla.org/en/DOM/window.setInterval
    • SO answer discussing the subtle difference between intervals and timeouts – https://stackoverflow.com/a/7900293/610573
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code where after a bool is true I want to
I have the following code to display the paragraph of a post, after the
I have following line of code to generate local notification after every 1 minute
I have the following php code from php.net <?php // The i after the
I’m after some C# code that will have the following methods that will return
I have following code in my application: // to set tip - photo in
I have following code in my Application. Comments in my code will specify My
I have following code in my application. [self.navigationController pushViewController:x animated:YES]; It will push a
I have following code class User attr_accessor :name end u = User.new u.name =
I am using jquery cycle for a slideshow. I cannot change the html code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.