0

This is my code (in a firefox addon)

this.something_something = function (the_actual_url) {
    this.this_version = null;
    try {
        // Firefox 4 and later; Mozilla 2 and later
        Components.utils.import("resource://gre/modules/AddonManager.jsm");
        AddonManager.getAddonByID("[email protected]", function (addon) {
            this_version = addon.version;
        });
    }
    catch (ex) {
        // Firefox 3.6 and before; Mozilla 1.9.2 and before
        var em = Components.classes["@mozilla.org/extensions/manager;1"].getService(Components.interfaces.nsIExtensionManager);
        var addon = em.getItemForID("[email protected]");
        this_version = addon.version;
    }


    this.timervar = setTimeout(function () {
        this.get_plugin_version(this.this_version);
    }, 2000);
}

this.get_plugin_version = function (blah) {
    alert("aa:" + blah);
}

I get the error:

Error: this.get_plugin_version is not a function Source File: chrome://mf_monkey_project/content/overlay.js Line: 476

What am I doing wrong?

Sorry about the screwed up formatting, but i deleted the bulk of the code to fit here and it made the formatting all screwy.

1 Answer 1

1

Because the setTimeout callback will be executed in the global context.

You can use the bind()[docs] method to bind the desired context and argument for the callback.

this.timervar=setTimeout( this.get_plugin_version.bind(this, this.this_version),
                          2000 );

If you don't want the current value of this.this_version permanently bound as the first argument, then remove it from the .bind() call.

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

6 Comments

now I get Error: this.get_plugin_version is undefined Source File: chrome://mf_monkey_project/content/overlay.js Line: 476
@Ryan: Then this in your something_something function is not the object you're expecting.
Oops, no, I did an edit instead of copying your code.. it works, but gives me null as the version number :( any idea why?
@Ryan: Because this.this_version = null;. If this_version is being updated within that 2000ms timer, then remove the this.this_version argument from the .bind() call. I updated my answer to reflect this.
Now I am getting some funny numbers, like 12 or 2341 or 2343 etc... but not the version number :(
|

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.