1

how can i add function on top of the already existing function?

i'm using following way and it gives me error on resources.onload

    resource = document.getElementById(this.id);

    if (resource && resource.getAttribute('data-loading'))
    {           
        onloadOthers = resource.onload;
        onloadThis   = this.onComplete.bind(this);

//following line give error

        resource.onload = function () { // callback loops again and again!!!!!!
            if (typeof onloadOthers == "function")
                onloadOthers();
            onloadThis();
        };

        return; // just added another callback, no need to add it again.
    }
    else if (resource)
    {           
        this.onComplete();
        return; // already exist
    }

    if (this.type == "js")
    { //if filename is a external JavaScript file
        var code = document.createElement('script');
        code.setAttribute("type", "text/javascript");
        code.setAttribute('data-loading', 'yes');
        code.setAttribute("src", this.file);
        code.onload = this.onComplete.bind(this);
        code.onreadystatechange = code.onload; // ie fix
    }
6
  • The error usually means a never ending loop Commented May 17, 2013 at 11:55
  • yes i know that. how can i fix it? i want to add previous callback on top of new callback. so they both get triggered onload. Commented May 17, 2013 at 11:56
  • 1
    onloadOthers may cause infinite recursion. Make sure that the unloadOthers variable is contained in a new closure. Commented May 17, 2013 at 11:56
  • @RobW i agree.. anyway to solve it? Commented May 17, 2013 at 11:57
  • 1
    @Basit Yes, put var before onloadOthers (assuming that the code you've shown is contained in function(){ and }, which created a closure. Currently, your code probably "recycles" the resource and onloadOthers (global) variables. By prefixing var, the variables will stay in the local scope. Commented May 17, 2013 at 11:59

1 Answer 1

1

Move the onloadOthers and resources variables in a new closure, by prefixing the var keyword.

Currently, your code "recycles" these (global) variables, because they're declared in a non-local scope:

var onloadOthers;
function func() {
    onloadOthers = ...;
    resource.onload = function() {
        onloadOthers(); // Calls the global `onloadOthers` function.
    };
}
func(); // Defines `onloadOthers`
func(); // Overwrites `onloadOthers`

By moving the variable to a local scope, all instances of the function will have an "own" onloadOthers variable, which solves the problem.

If you want to learn more about this topic, read How do JavaScript closures work?.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.