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 6234151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:25:57+00:00 2026-05-24T10:25:57+00:00

I need to run the code sample below so that it works on a

  • 0

I need to run the code sample below so that it works on a toggle, so when the div is clicked it starts the image rotation and when it is clicked again it starts and so on?
Thanks in advance 🙂

 $('#pause_button1').click(function() {

    /* need to run this when clicked - need a toggle */ 

        function changeImage(element, imageNumber){
            imageNumber++;

            if(imageNumber > 6){
                imageNumber = 1;
            }
            console.log(imageNumber);
            element.addClass('pause_button' + imageNumber, 200);

            element.attr('class', 'pause_button' + imageNumber);
            setTimeout(function(){changeImage(element, imageNumber)}, 200);  
        }

        changeImage($('#pause_button'), 0);    
}); 

example: jsfiddle

if you look at this example: jsfiddle.net/AKgZn/2 it needs to cycle when the white square (#pause_button1) is clicked and when its cycling and you click the cycling square again it returns to the white square

  • 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-05-24T10:25:58+00:00Added an answer on May 24, 2026 at 10:25 am

    Try this:

    html:

    <div id="pause_button" class="pause_button1">
    

    javascript:

    $('#pause_button').click(function() {
    
        //Initially sets global variable is_toggled to true, then
        //toggles it back and forward to true and false
        window.is_toggled = !window.is_toggled;
        if (window.is_toggled) { 
          function changeImage(element, imageNumber){
    
              imageNumber++;
    
              if(imageNumber > 6){
                imageNumber = 1;
              }
              console.log(imageNumber);
              element.attr('class','pause_button' + imageNumber);
              window.timeOutId = setTimeout(function(){changeImage(element, imageNumber)}, 200); 
          }
          changeImage($('#pause_button'), 0);    
      } else {
           clearTimeout(window.timeOutId);
           $('#pause_button').attr('class','pause_button' + 1);
      }  
    });
    

    The key is the global variable window.is_toggled, although you can use any name: window.my_variable, or whatever. You don’t need to initially declare it with “var” as, fortunately, !undefined = true.

    Hope that helps? Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need run code that will create a database and populate tables. i am
I need to run some code that will fetch some configuration values from the
I need to create a desktop application that will run third party code, and
I'm trying to run the sample unit tests for the sample code that ships
[Rails] I need to run some code only 40% of the times, how may
I have a need to run different code when a user clicks or double-clicks
I need to run a PHP code snippet in a Drupal template and prevent
I need to run an external application from within my Java code. I can
I need to run a MonteCarlo simulations in parallel on different machines. The code
I need help optimizing the code to run faster, unless it is optimized the

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.