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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:35:16+00:00 2026-06-13T09:35:16+00:00

I have some code where I have a timer. Every 3 seconds the background

  • 0

I have some code where I have a timer. Every 3 seconds the background changes colors, and when you hover the stop button, the color changer pauses, I have an onclick event that i am using in junction with the mouseout event but the mouseout event cancels out my onclick event. What can I do so that the onclic event works still after I move the mouse from the stop button?

Code: jsfiddle

<script>
var colors = new Array();
colors[0] = "green";
colors[1] = "blue";
colors[2] = "gray";
var i = 0;
var timer;

function changeOfPlans() {
    timer = setInterval("colorChange()", 3000);
}

function colorChange() {
    document.getElementById("one").style.backgroundColor = colors[i];
    document.getElementById("two").style.backgroundColor = colors[i];
    i++;
    if (i == 3 || i > 3) {
        //start over by setting i to 0
        i = 0;
    }
}

function stop() {
    clearTimeout(timer);
}​
</script>
  • 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-13T09:35:17+00:00Added an answer on June 13, 2026 at 9:35 am

    You need to update the on click action so that it sets a variable. Then have the mouse out first check whether that variable is set before restarting the color changing. Demo.

    I modified your JavaScript like so:

    var stopped = false;
    
    function changeOfPlans() {
        if (!stopped) {
            timer = setInterval("colorChange()",3000);
        }
    }
    
    function fullstop() {
      stopped = true;
      stop();
    }
    

    Then I updated your onclick to call fullstop() instead of just stop(). Since I left the other stop() function the same, the hover-to-stop-move-away-to-restart functionality still works as you had it originally. All of the other JavaScript remains the same.

    <button type="button" onmouseover="stop()" onmouseout="changeOfPlans()" onclick="fullstop()">Stop</button>
    

    There are other ways of doing this — enhzflep suggests a good one in the comments — but this is the simplest.

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

Sidebar

Related Questions

I have some code that will change the background color of a specific label
I have some code which takes strings representing hexadecimal numbers - hex colors, actually
I have some code where the timer EventHandler has this void timer_Tick(object sender, EventArgs
I have a background operation I would like to occur every 20 seconds in
I've developed some code that implements a timer - it ticks every second and
I have an application that start System.Threading.Timer, then this timer every 5 seconds read
I have found some code on measuring execution time here http://www.dreamincode.net/forums/index.php?showtopic=24685 However, it does
I'm writing some code here, and I'm having a had time. I have a
I have some code on two systems running kernel 2.4.20 and kernel 2.4.38 .
I have some code here that uses bitsets to store many 1 bit values

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.