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

  • SEARCH
  • Home
  • 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 9035809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:50:38+00:00 2026-06-16T08:50:38+00:00

I am toggling each 4 divs for 10 seconds using this jQuery function based

  • 0

I am toggling each 4 divs for 10 seconds using this jQuery function based on a href id value.

The timer works fine and it changes 4 divs for every 10 seconds but when user clicks a particular div it doesn’t navigate to particular div and stays there for a given period(ex 10 sec) and proceeds to next div from current div instead of that it goes to div based on timer value.

Can anyone help me regarding this?

$(function() {
    $("a.menu").click(function() {
        $("div.featuredposts_content").hide();
        $($(this).attr('href')).show();
        return false;
    });
});

$(function() {
    var counter = 0,
        divs = $('#cat1, #cat2, #cat3,#cat4');

    function showDiv() {
        divs.hide() // hide all divs
        .filter(function(index) {
            return index == counter % 4;
        }) // figure out correct div to show
        .show('fast'); // and show it
        counter++;
    }; // function to loop through divs and show correct div
    showDiv(); // show first div    
    setInterval(function() {
        showDiv(); // show next div
    }, 15 * 1000); // do this every 10 seconds    
});​
  • 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-16T08:50:39+00:00Added an answer on June 16, 2026 at 8:50 am

    I assumed the exact mark-up from the fragments posted in the comments.

    First I think you should re-set your counter after each full cycle otherwise it will grow, grow, grow and eventual could cause an exception, maybe not within the first 2-3 days but eventually. There is no need to have the counter variable go beyond divs.length.

    Aside that the only thing really missing from your code was the setting of the counter value inside your click event:

    $("a.menu").click(function() {
        $("div.featuredposts_content").hide();
        var $divToShow = $($(this).attr('href'));
        $divToShow.show();
        
        // save the index of the new div +1 to ensure it moves on to the div thereafter
        counter = divs.index($divToShow) + 1; 
        
        return false;
    });
    

    DEMO – Timer continues from selected div

    (I shortened the timer in the DEMO for demonstration purpose you should off course re-set it to your original value)

    I used the following assumed HTML:

    <a href="#cat1" class="menu">cat1</a>
    <a href="#cat2" class="menu">cat2</a>
    <a href="#cat3" class="menu">cat3</a>
    <a href="#cat4" class="menu">cat4</a>
    <div id="cat1" class="featuredposts_content">Category1</div>
    <div id="cat2" class="featuredposts_content">Category2</div>
    <div id="cat3" class="featuredposts_content">Category3</div>
    <div id="cat4" class="featuredposts_content">Category4</div>
    

    ​
    This is the complete script:

    $(function() {
        var counter = 0;
        var divs = $('#cat1, #cat2, #cat3, #cat4');
    
        function showDiv() {
            divs.hide() // hide all divs
            .filter(function(index) {
                return index == counter % divs.length;
            }) // figure out correct div to show
            .show('fast'); // and show it
            if(counter == divs.length){
                counter = 1;
            } else {
                counter++
            }
        }; // function to loop through divs and show correct div
        showDiv(); // show first div    
        setInterval(function() {
            showDiv(); // show next div
        }, 2000); // do this every 10 seconds    
        
        $("a.menu").click(function() {
            $("div.featuredposts_content").hide();
            var $divToShow = $($(this).attr('href'));
            $divToShow.show();
            
            counter = divs.index($divToShow) + 1;
            
            return false;
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(Jquery) I have two divs that I'm toggling back and forth (show and hide)
I am using this code the create an onshow event: (function($){ $.fn.extend({ onShow: function(callback,
I'm using this piece of jQuery to add radio button behavior to checkboxes, i.e.
I'm using Unobtrusive Toggling in a view that contains many records (Orders). I've got
I'm new to javascript/jQuery this really has me stumped. What I'm trying to achieve
I'm trying to write a plugin automates toggling stuffs for panels in jQuery. Everything
I have a page with five links. Each link opens up a lightbox (using
I got this HTML structure: <form> <input class=myTrigger type=radio value=1/> <input class=myTrigger type=radio value=2/>
My toggle function is only toggling through 2 of the 3 classes and not
so i'm calling a function in jquery that's looping over a table and determining

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.