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

标签 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)

有什么想法可以调用该方法吗?

最佳答案

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

Prototypes当使用new实例化该函数(类)时,用于向新对象添加方法运算符:

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

...

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

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

关于javascript - 如何调用 Javascript 方法(超出范围?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6783193/

相关文章:

javascript - 开 Jest 模拟默认导出 - 需要与导入

javascript - jQueryUI 自动完成默认文本

c# - 如何使添加到现有标记的控件自动连接到后面的代码?

c# - 无法动态地将单元格添加到行中

javascript - JavaScript Array 的内存管理

javascript - 如何使用 react 在两列或多列中显示 map 的结果

javascript - 如何实现节点模拟退火(TSP)

javascript - Jquery:最近()行删除不起作用

javascript - 动态附加 DIV 并使用 JQuery 中的类名计算 div 内的数据

c# - 在 iis 和 asp.net 中支持虚 url 所需的工作流