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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:25:39+00:00 2026-05-19T22:25:39+00:00

I have a jQuery plugin that uses a needle gauge. Upon initially loading a

  • 0

I have a jQuery plugin that uses a needle gauge. Upon initially loading a page containing the plugin, the plugin begins its animation before the background image has loaded. A second image (a “broken” gauge) loads when the speedometer goes out of range, but again there is a delay before the image loads. (to see this, enter a value lower than zero or higher than 100)

http://jacob-king.com/demo/speedometer

I tried using this solution, which solves the issue of the “broken” image preloading:

http://jacob-king.com/public/notSoPublic/jquery.speedometer-1.0.3/example.html

(function(){

     // counter
     var i = 0;

     // create object
     imageObj = new Image();

     // set image list
     images = new Array();
     images[0]=_speedometerDirectory+"/background.jpg";
     images[1]=_speedometerDirectory+"/background-broken.jpg";

     // start preloading
     for(i=0; i<=1; i++) 
     {
          imageObj.src=images[i];
     }

})();

But how do I force the plugin to wait for both background images to load before starting the animation?


Solution

Since the images were loaded as part of the jQuery plugin, placing them outside the $.fn call seemed inappropriate; the images shouldn’t load unless the speedometer function is invoked.

I ended up going with a slightly modified version of crimson_penguin’s solution:

var img0 = new Image();
img0.src = _speedometerDirectory+"/background.jpg";
var img1 = new Image();
img1.src = _speedometerDirectory+"/background-broken.jpg";

$(img0).load(function() {
    $(img1).load(function() {
        // Everything's ready. Now we begin.
    });
});

This probably looks a little kludgy. Feel free to suggest a more elegant syntax (It’s late and I’m tired, and this works, so “Good Enough”). The result can be seen here, and I’ll be including it as part of my 1.0.4 update. Thanks, everyone.


Edit: I spoke too soon. Without the images loaded into cache, the plugin fails to start on the first page load in Google Chrome…

  • 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-19T22:25:39+00:00Added an answer on May 19, 2026 at 10:25 pm

    You’re only using one object to load both images; I’m not sure that will work. I would instead do something like this:

    for(i=0; i<=1; i++) 
    {
         var img = new Image();
         img.src = images[i];
         images[i] = img;
         $(images[i]).load(function() { ... });
    }
    

    Then you can set that load function there to decrement (or increment) some count variable, and when it reaches zero (or the number it’s supposed to reach), you start. The count variable will have to not be local to that for loop of course. I’m not sure if it has to be global or if it could be local to the function.

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

Sidebar

Related Questions

I have a web page that uses a large image for its background. I
I have been working on a jquery plugin that uses a HTML5 audio player
I currently have a site that uses the JQuery cycle plugin. The site uses
We have developed a website that uses MVC, C#, and jQuery. In one of
I'm playing around with writing a jQuery plugin that uses an attribute to define
I have created a jQuery plugin that works great with the exception of being
I am using a plugin that uses jquery to pull an rss feed, and
I am currently working on rails3 application that uses jQuery. I have a javascript
Good afternoon people's I have built a jquery plugin that I use on my
How do you rotate an image using jQuery-rotate plugin? I have tried the following

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.