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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:05:11+00:00 2026-06-04T01:05:11+00:00

How do you stop a timer when clearInterval() does not stop it? The purpose

  • 0

How do you stop a timer when clearInterval() does not stop it?

The purpose of this code is to animate a number from 0 upwards until it reaches the end (eg animate from 0… 75%). But the timer does not stop when I call clearInterval():

http://jsfiddle.net/pwYEe/2/

<div id="foo"></div>
<div id="bar"></div>

animate("99%", $("#foo")); // doesnt stop
animate("75%", $("#bar")); // doesnt stop

function loop(clr, clr2, ele, rand, last, delay) {
    clearInterval(clr);
    clearInterval(clr2);
    inloop(clr, clr2, ele, rand, last, delay);

    if (rand >= last) {
        clearInterval(clr);
        clearInterval(clr2);
    }
    else {
        clr2 = setTimeout(function () {
            loop(clr, clr2, ele, rand, last, delay);
        }, 2500);
    }

}
function inloop(clr, clr2, ele, rand, last, delay) {
    ele.html((rand += 1) + "%");

    if (rand >= last) {
        clearInterval(clr);
        clearInterval(clr2);
    }
    else {
        clr = setTimeout(function () {
            inloop(clr, clr2, ele, rand, last, delay);
        }, delay);
    }
}

function animate(end, display) {
    var clr = null;
    var clr2 = null;
    var ele = null;
    var rand = 0;  // initial number
    var last = 99; // last number
    var delay = 5; // inner loop delay

    ele = display;
    ele.html("0%");

    var idx = end.indexOf("%");
    if (idx >=0) {
        end = end.substring(0,idx);
    }
    last = parseInt(end);
    loop(clr, clr2, ele, rand, last, delay);
}
  • 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-04T01:05:12+00:00Added an answer on June 4, 2026 at 1:05 am

    You use clearTimeout() with setTimeout().

    You don’t use clearInterval() with setTimeout(). clearInterval() goes with setInterval().

    You also have a structural problem in your code. You are passing around clr and clr2 as arguments and expecting the original values of those to be modified and they are not.

    You can fix that by putting them both in an object and passing the object like this:

    animate("99%", $("#foo"));
    //animate("75%", $("#bar"));
    
    function loop(timers, ele, rand, last, delay) {
        clearTimeout(timers.clr);
        clearTimeout(timers.clr2);
        inloop(timers, ele, rand, last, delay);
    
        if (rand >= last) {
            clearTimeout(timers.clr);
            clearTimeout(timers.clr2);
        }
        else {
            timers.clr2 = setTimeout(function () {
                loop(timers, ele, rand, last, delay);
            }, 2500);
        }
    
    }
    function inloop(timers, ele, rand, last, delay) {
        ele.html((rand += 1) + "%");
    
        if (rand >= last) {
            clearTimeout(timers.clr);
            clearTimeout(timers.clr2);
        }
        else {
            timers.clr = setTimeout(function () {
                inloop(timers, ele, rand, last, delay);
            }, delay);
        }
    }
    
    function animate(end, display) {
        var timers = {clr: null, clr2: null};
        var ele = null;
        var rand = 0;  // initial number
        var last = 99; // last number
        var delay = 5; // inner loop delay
    
        ele = display;
        ele.html("0%");
    
        var idx = end.indexOf("%");
        if (idx >=0) {
            end = end.substring(0,idx);
        }
        last = parseInt(end);
        loop(timers, ele, rand, last, delay);
    }
    ​
    

    Working jsFiddle: http://jsfiddle.net/jfriend00/PEqeY/

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

Sidebar

Related Questions

I have this code: private void timer_Tick(object sender, EventArgs e) { timer.Stop(); for (int
How do you I stop my links like this: <a href=# onClick=submitComment()> From jumping
I want to stop Timer at some point and then resume it from the
I want to start/stop timer(in main class) with switch(from menu class ),based in my
The problem is with object's variable: this.timer it's not global, so when I click
Possible Duplicate: NSTimer doesn't stop I have this code: [NSTimer scheduledTimerWithTimeInterval:110.0 target:self selector:@selector(targetMethod:) userInfo:nil
I tried this flex example from blog.flexexamples.com . This is a basic timer example.
I want to stop click on any links until the page is loaded. Can
When you start/stop a python cherrypy service (compiled with py2exe), this works fine When
How do i stop the header template of a repeater from displaying when there

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.