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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:15:53+00:00 2026-05-13T21:15:53+00:00

Hay guys, I’m making a simple preload function (function($) { $.fn.preload = function(settings) {

  • 0

Hay guys, I’m making a simple preload function

(function($) {
    $.fn.preload = function(settings) {

    var config = {
        before: function(){ return; },
        end: function(){ return; },
        after: function(a){ return; }
    };

    if (settings) $.extend(config, settings);

    var limit = this.length - 1;
    var counter = 0;

    settings.before();

    is_last_done = function(i){if (i == limit) return true;};

    this.each(function(i, src){
        $("<img>").attr("src", src).load(function(){

            if( a == counter ) settings.after();
            if(is_last_done(i)) settings.end(); // Last is done
            counter++;

        });
    });
    return this;
};
})(jQuery);

and calling it with

    img = ['a.jpg', 'b.jpg', 'b.jpg'];

    a = 1;

    $(img).preload({
        before: function(){ return; },
        end: function(){  },
        after: function(a){
            console.log('first done');
        }
    });

the problem is that the ‘a’ variable im passing to the ‘after()’ function is not being passed.

Inside the plugin i can access the variable with ‘a’, like on this line

if( a == counter ) settings.after();

The ‘a’ is availabe, however what if i want to name the a variable to something else? How do i access the argument of the after() function?

My code won’t work anymore if i use this code

img = [‘a.jpg’, ‘b.jpg’, ‘b.jpg’];

b = 1;

$(img).preload({
    before: function(){ return; },
    end: function(){  },
    after: function(b){
        console.log('first done');
    }
});

b doesnt get passed, any ideas?

Thanks

  • 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-13T21:15:53+00:00Added an answer on May 13, 2026 at 9:15 pm

    If I understand correctly, you want to pass the iteration index to .after().

    In your plugin, variable counter is used to track which step of the each() iteration you are in. This is unnecessary, as the first parameter to each is the loop index. See each in the documentation.

    Also, function is_last_done() is not needed as the limit variable is in scope for the anonymous function called by each().

    var limit = this.length - 1;
    
    settings.before();
    
    this.each(function(i, src){
        $("<img>").attr("src", src).load(function(){
    
            if (i == a) settings.after(i);
            if (i == limit) settings.end();
    
        });
    });
    

    Javascript is lexically scoped, so you do not have to do anything for a function to see variables declared in enclosing scopes. This is properly termed closure, and is described in this Mozilla article – Working with Closures.

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

Sidebar

Ask A Question

Stats

  • Questions 444k
  • Answers 444k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It is possible to do with all 3 libraries you… May 15, 2026 at 6:39 pm
  • Editorial Team
    Editorial Team added an answer It's not that Object-orient Programming is "non-Procedural"; it's just that… May 15, 2026 at 6:39 pm
  • Editorial Team
    Editorial Team added an answer I did some testing and I feel that the best… May 15, 2026 at 6:39 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.