如何调用Javascript方法(超出范围?)

时间:2011-07-21 21:43:00

标签: javascript jquery asp.net

我正在使用this ASP.Net/jQuery session timeout control。效果很好,但我需要从jQuery对话框以外的某个地方访问其中一个javascript方法。以下是我要访问的代码段:

TSC.Timeout.Timeout.prototype =
{
    // THE METHOD I WANT TO CALL: _resetTimeout()
    _resetTimeout: function (e) {
        // modify timeout to do jquery dialog
        if (typeof jQuery.ui == 'undefined')
            $get(this._clientId).style.display = 'none';

        clearTimeout(this._timerAboutToTimeout);
        clearTimeout(this._timerTimeout);
        clearTimeout(this._timerCountDown);

        this._showAboutToTimeoutDelegate = Function.createDelegate(this, this.showAboutToTimeout);
        this._timerAboutToTimeout = setTimeout(this._showAboutToTimeoutDelegate, this._aboutToTimeoutMinutes * 5 * 1000); //TODO: Change this back to 60
        this._timeoutDelegate = Function.createDelegate(this, this.timeout);
        this._timerTimeout = setTimeout(this._timeoutDelegate, this._timeoutMinutes * 10 * 1000); //TODO: Change this back to 60
    },

    // HOW IT'S BEING CALLED FROM WITHIN THE JS OBJECT:
    initDialog: function (e) {
        // modify timeout to do jquery dialog
        if (typeof jQuery.ui != 'undefined') {
            var tsc = this;
            $("#" + this._clientId).dialog({
                autoOpen: false,
                width: 500,
                resizeable: false,
                bgiframe: true,
                modal: true,
                position: 'center',
                buttons: {
                    "Keep Me Signed In": function () {
                        $(this).dialog('close');
                        CallServer();
                        tsc._resetTimeout();
                    }
                }
            });
        }
    }
}

我似乎无法从控制台获取_resetTimeout()。调用TSC.Timeout.Timeout.prototype._resetTimeout();会产生以下错误:

Uncaught TypeError: Cannot read property 'length' of undefined
TSC.Timeout.Timeout.showAboutToTimeoutWebResource.axd:200
(anonymous function)ScriptResource.axd:47
WebResource.axd:217Uncaught TypeError: Cannot read property 'length' of undefined
TSC.Timeout.Timeout.timeoutWebResource.axd:217
(anonymous function)ScriptResource.axd:47
WebResource.axd:213Uncaught TypeError: Property 'focus' of object [object DOMWindow] is not a function
TSC.Timeout.Timeout.showAboutToTimeoutWebResource.axd:213
(anonymous function)

我怎么称呼这种方法?

1 个答案:

答案 0 :(得分:3)

调用TSC.Timeout.Timeout.prototype._resetTimeout();调用原型上的原始方法 - 换句话说,在范围内使用对象。

当使用new运算符实例化Function(类)时,

Prototypes用于向新对象添加方法:

var timer = new TSC.Timeout.Timeout();

...

timer._resetTimeout(); // Reset timeout called with "timer" object in scope

通常,名称前面的_表示它是“私有”或内部方法。这意味着功能是通过其他API提供的,不需要直接调用,可能会产生意外结果。所以,我会检查以确保没有其他方法可以完成你需要做的事情(我不知道你是否提供了完整的源代码......)。