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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:02:18+00:00 2026-06-01T03:02:18+00:00

I am working on displaying a progress bar using JQuery during a long running

  • 0

I am working on displaying a progress bar using JQuery during a long running function. My plan is to simply show the div containing the progress bar before calling the function, and then hiding the div after the function is finished. The following code example demonstrates what I am trying to accomplish.

$(document).ready(function() {
    $(document).click(function() {
        $("#progress").show();
        slowFunction();
        $("#progress").hide();
    });
});

function slowFunction() {
    for (var i = 0; i < 50000; ++i) {
        var t = Math.sin(i) + Math.cos(i);
        console.log(t);
    }
}

Example on JSFiddle http://jsfiddle.net/MT25x/4/

The problem is that the div is not being displayed at all even though the function takes ~10 seconds on my machine to run.

If however I just try to show the div after the function it works fine, coming up in ~10 or so seconds after the click occurs. Can anybody help me shed some light on this issue?

Edit:
Further testing seems to show that if you remove the hide call all together the div isn’t even showing until after the function has finished running. This seems like very odd behavior.

  • 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-01T03:02:20+00:00Added an answer on June 1, 2026 at 3:02 am

    This is a common problem with most browsers. When a cpu intensive function runs it locks the main executing thread and the browser doesn’t do a repaint until the function has finished executing, hence the div is getting hidden and shown really fast because both show and hide were queued. To circumvent this you can use thesetTimeout(...,0) trick, this queues the function for immediate execution. This is not guaranteed to work cross browser and i have only tested this on ie, chrome and firefox.

    $(document).ready(function() {
        $(document).click(function() {
            setTimeout(function(){$("#progress").show()},0); // queue this for immediate execution
            setTimeout(function(){slowFunction()}, 2000); // delay the script a little giving the browser time to repaint                           
        });
    });
    
    function slowFunction() {
    
        for (var i = 0; i < 500; ++i) {
            var t = Math.sin(i) + Math.cos(i);
        }
        $("#progress").hide();
    }
    

    Here’s a fiddle.
    ​

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

Sidebar

Related Questions

I am using following code, but my progress bar is not displaying in the
A dialog I'm working on isn't displaying, using: CWnd::CreateDlgIndirect(LPCDLGTEMPLATE lpDialogTemplate,CWnd* pParentWnd, HINSTANCE hInst) The
I've been working on div with rounded corners. I've got it displaying properly in
When displaying progress bars do you display generic messages, such as: Working Loading In
I'm working on displaying several batches of markers using the Google Maps api (v3)
I'm working on a console app that kicks off a fairly long running process
i am working on displaying an image and placing an icon on top of
I am writing a stored procedure for displaying month and year. It is working,
I have a little trouble with a progress bar since iOS 5 came out.
I'm working on displaying lines numbers for a richTextBox on C#, I tried many

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.