访问外部的jQuery闭包函数
Accessing a jQuery Closure function outside
我在文件中嵌入了这段代码,用于管理会话超时。这是从http://www.fairwaytech.com/2012/01/handling-session-timeout-gracefully/
我想为所有ajax请求调用SessionManager.extend()完成所以我可以自动刷新我的会话管理器时间。
这就是我尝试的
<script type="text/javascript">
$(document).ajaxSuccess(function (event, xhr, settings) {
if (xhr.status === 200) {
SessionManager().extend();
}
});
</script>
正在获取找不到SessionManager对象的错误。我们怎么称呼它
以下是从该站点获取的库代码
$(function () { // Wrap it all in jQuery documentReady because we use jQuery UI Dialog
// HtmlHelpers Module
// Call by using HtmlHelpers.getQueryStringValue("myname");
var HtmlHelpers = function () {
return {
// Based on http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript
getQueryStringValue: function (name) {
var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
return match && decodeURIComponent(match[1].replace(/'+/g, ' '));
}
};
} ();
// StringHelpers Module
// Call by using StringHelpers.padLeft("1", "000");
var StringHelpers = function () {
return {
// Pad string using padMask. string '1' with padMask '000' will produce '001'.
padLeft: function (string, padMask) {
string = '' + string;
return (padMask.substr(0, (padMask.length - string.length)) + string);
}
};
} ();
// SessionManager Module
var SessionManager = function () {
// NOTE: globalTimeoutPeriod is defined in _Layout.cshtml
var sessionTimeoutSeconds = HtmlHelpers.getQueryStringValue('smt') || (globalTimeoutPeriod),
countdownSeconds = HtmlHelpers.getQueryStringValue('smc') || 300,
secondsBeforePrompt = sessionTimeoutSeconds - countdownSeconds,
$dlg,
displayCountdownIntervalId,
promptToExtendSessionTimeoutId,
originalTitle = document.title,
count = countdownSeconds,
extendSessionUrl = '/Session/Extend',
expireSessionUrl = '/Session/Expire?returnUrl=' + location.pathname;
var endSession = function () {
$dlg.dialog('close');
location.href = expireSessionUrl;
};
var displayCountdown = function () {
var countdown = function () {
var cd = new Date(count * 1000),
minutes = cd.getUTCMinutes(),
seconds = cd.getUTCSeconds(),
minutesDisplay = minutes === 1 ? '1 minute ' : minutes === 0 ? '' : minutes + ' minutes ',
secondsDisplay = seconds === 1 ? '1 second' : seconds + ' seconds',
cdDisplay = minutesDisplay + secondsDisplay;
document.title = 'Expire in ' +
StringHelpers.padLeft(minutes, '00') + ':' +
StringHelpers.padLeft(seconds, '00');
$('#sm-countdown').html(cdDisplay);
if (count === 0) {
document.title = 'Session Expired';
endSession();
}
count--;
};
countdown();
displayCountdownIntervalId = window.setInterval(countdown, 1000);
};
var promptToExtendSession = function () {
$dlg = $('#sm-countdown-dialog')
.dialog({
title: 'Session Timeout Warning',
height: 250,
width: 350,
bgiframe: true,
modal: true,
buttons: {
'Continue': function () {
$(this).dialog('close');
refreshSession();
document.title = originalTitle;
},
'Log Out': function () {
endSession(false);
}
}
});
count = countdownSeconds;
displayCountdown();
};
var refreshSession = function () {
window.clearInterval(displayCountdownIntervalId);
var img = new Image(1, 1);
img.src = extendSessionUrl;
window.clearTimeout(promptToExtendSessionTimeoutId);
startSessionManager();
};
var startSessionManager = function () {
promptToExtendSessionTimeoutId =
window.setTimeout(promptToExtendSession, secondsBeforePrompt * 1000);
};
// Public Functions
return {
start: function () {
startSessionManager();
},
extend: function () {
refreshSession();
}
};
} ();
SessionManager.start();
});
从SessionManager中删除var前缀。
这里有一些关于范围的信息,http://msdn.microsoft.com/en-us/library/ie/bzt2dkta(v=vs.94).aspx
相关文章:
- 使用闭包/函数绑定将自函数作为回调传递
- 了解 JavaScript 闭包函数
- 传递多个参数的闭包函数
- 将“(0)”附加到此闭包函数的目的是什么
- 如何复制(新实例)闭包函数
- Javascript 闭包函数参数
- 具有闭包函数作用域的意外行为
- javascript闭包函数可以由用户更改吗
- Javascript:是这个闭包函数
- 为什么闭包函数中的 javascript 变量在多次调用时不会重置为默认值
- Js闭包:函数在第二次调用时失败
- 如何存储闭包函数返回的变量
- javascript闭包函数的用法
- JavaScript闭包函数传递给事件监听器
- 闭包函数问题
- & # 39;这个# 39;匿名闭包函数内部
- 在JavaScript中实现自动记忆(返回闭包函数)
- 不理解闭包函数是如何工作的
- Javascript闭包函数
- 为javascript闭包函数编写jasmine测试规范