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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:10:19+00:00 2026-06-14T05:10:19+00:00

I have a function that runs on a click event that uses javascript’s setIterval

  • 0

I have a function that runs on a click event that uses javascript’s setIterval for some of my animations (i’m doing a game) so the problem is that if a user clicks while the animation is still displaying (setInterval is still executing) the setInterval is stacking up in the event stack or that is what I found out thus either crushing my game or running twice as fast (the animation). My question is is there any way to prevent event stacking? I do not want the setInterval to stack up on the previous setInterval and so on. I know that I could use clearInterval function like so:

  var timerInterval = setInterval(drawPlayerRunning, 50);
  clearInterval(timerInterval);

but it does not really work as I want it to, because what if user clicks many times while the function is still is executing, the clearInterval will only get rid of last event of the event stack leaving all the previous ones still in the “game”. Any idea how to prevent this event stack up, or at least removing them efficiently?

  • 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-14T05:10:21+00:00Added an answer on June 14, 2026 at 5:10 am

    You can create a flag that monitors the interval state:
    1)

    var isIntervalInProgress = false;
    setInterval(function()
    {
      if ( isIntervalInProgress )
        return false;
    
      isIntervalInProgress = true;
    
      drawPlayerRunning();
    
      isIntervalInProgress = false;
    
    }, 50);
    

    or just a timeout that will run itself once it’s finished:
    2)

    var func = function()
    {
      setTimeout(function()
      {
        drawPlayerRunning();
        func();
      }, 50)
    }
    

    whichever you like

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

Sidebar

Related Questions

I have a setup function that runs onload to add some behaviours to elements.
I have a javascript function that runs every 3 seconds, is there any way
I have a form button with an onclick event that runs a simple javascript
I have some jQuery code that runs on every $(document).ready() event. I also have
problem page: www.hovaness.com/work I have a function with variable name sliderun(); that runs a
Inside my Controller i have function that runs after user clicks on item, which
I have a function that takes an input string and then runs the string
I have this function that takes the input of a, runs a calculation and
In Winforms you just have the Main function and that's what runs first, but
I have function Start() that is fired on ready. When I click on .ExampleClick

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.