我正在使用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/