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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:56:45+00:00 2026-06-08T18:56:45+00:00

Below is the source code for a function that preloads images onto a page,

  • 0

Below is the source code for a function that preloads images onto a page, the author has added in some comments to explain how the code works, but I still do not fully understand all of it. to be specific, He claims that the return value of this function is an empty object with a “done()” method that calls the predefined anonymous function, “postaction()”. Is the user of this code supposed to enter his/her own code into the empty postaction function on line 2? If that’s how it works, then what does “postaction=f || postaction” in the return object do?

Source code:

function preloadimages(arr){
  var newimages=[], loadedimages=0
  var postaction=function(){}
  var arr=(typeof arr!="object")? [arr] : arr
  function imageloadpost(){
      loadedimages++
      if (loadedimages==arr.length){
          postaction(newimages) //call postaction and pass in newimages array as parameter
      }
  }
  for (var i=0; i<arr.length; i++){
      newimages[i]=new Image()
      newimages[i].src=arr[i]
      newimages[i].onload=function(){
          imageloadpost()
      }
      newimages[i].onerror=function(){
        imageloadpost()
      }
  }
  return { //return blank object with done() method
      done:function(f){
          postaction=f || postaction 
          //remember user defined callback functions to be  called when images load
    }
  }
}

link to author’s page:http://www.javascriptkit.com/javatutors/preloadimagesplus.shtml

  • 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-08T18:56:46+00:00Added an answer on June 8, 2026 at 6:56 pm

    You can enter your own function for postaction, although it’s not a callback function like you might expect.

    However, it returns an object with a done function in it.

    If you do soth. like

     preloadimages().done(function () {
         console.log('done')
     });
    

    your function will be executed. If you don’t provide the fnction as a parameter of done, the default postaction will be called and do nothing, since it is an empty function

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

Sidebar

Related Questions

I'm running the below command to show the source code for a PostgreSQL function,
I am trying to write a program to check that some C source code
In the source code below, I am selecting a subset of properties from the
Given the example source code below, is it possible for someone to see the
in Linux kernel source code, I find below code: h++; pending >>= 1; It
I'd like to retrieve the image source within the below HTML code block, but
I've attached a source file below, I'm kinda stuck. Basically, its an FLA that
I've been having some difficulty in understanding the source of a problem. Below is
I have the below code that does not seem to work at all :(
how can i grab the arguments of a javascript function? below is my code,

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.