如何调用Javascript方法(超出作用域?)

How to call Javascript method (out of scope?)

本文关键字:作用域 方法 Javascript 调用 何调用      更新时间:2023-09-26

我正在使用这个ASP。Net/jQuery会话超时控制。工作很棒,但是我需要从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)

知道如何调用这个方法吗?

调用TSC.Timeout.Timeout.prototype._resetTimeout();调用原型上的raw方法——换句话说,作用域中没有对象。

原型用于在使用new操作符实例化函数(类)时向新对象添加方法:

var timer = new TSC.Timeout.Timeout();
...
timer._resetTimeout(); // Reset timeout called with "timer" object in scope

通常,名称前的_表示它是"private",或内部方法。这意味着该功能是通过其他api提供的,不需要直接调用,并且可能产生意想不到的结果。因此,我会检查以确保没有其他方法来完成您需要做的事情(我不知道您是否提供了完整的源代码或不…)。