将 window.SessionTimeout 脚本传递给 js 文件 [英] Pass window.SessionTimeout script to js file
问题描述
我正在做一个 MVC 应用程序.这是在我的 _Layout.cshtml
I´m doing an MVC app. And this is in my _Layout.cshtml
我需要把它移到一个js文件中
I need to move it to a a js file
<script type="text/javascript">
window.SessionTimeout = (function() {
var _timeLeft, _popupTimer, _countDownTimer;
var stopTimers = function() {
window.clearTimeout(_popupTimer);
window.clearTimeout(_countDownTimer);
};
var updateCountDown = function() {
var min = Math.floor(_timeLeft / 60);
var sec = _timeLeft % 60;
if(sec < 10)
sec = "0" + sec;
document.getElementById("CountDownHolder").innerHTML = min + ":" + sec;
if(_timeLeft > 0) {
_timeLeft--;
_countDownTimer = window.setTimeout(updateCountDown, 1000);
} else {
document.location = "Home/TimeOutPage";
}
};
var showPopup = function() {
_timeLeft = 60;
updateCountDown();
ClientTimeoutPopup.Show();
};
var schedulePopup = function() {
stopTimers();
_popupTimer = window.setTimeout(showPopup, @PopupShowDelay);
};
var sendKeepAlive = function() {
stopTimers();
ClientTimeoutPopup.Hide();
SessionTimeout.schedulePopup();
};
return {
schedulePopup: schedulePopup,
sendKeepAlive: sendKeepAlive
};
})();
我尝试只复制脚本标记之间的代码,但它不起作用.我知道问题在于函数签名
I try to copy only the code between the script tag but it doesn't work. I know the problem is with the function signature
window.SessionTimeout = (function() { ...
但我不知道在 js 文件中使用 window.SessionTimeout 的热度.
but i don´t know hot to use window.SessionTimeout in a js file.
@PopupShowDelay 在我看来是这样定义的:
The @PopupShowDelay is define in my view like this:
@functions {
public int PopupShowDelay
{
get { return 60000 * (Session.Timeout - 1); }
}
}
更新
好的,我发现了错误.问题是变量
Ok, i found the error. The problem is the variable
@PopupShowDelay
我在 view 中定义了它,并试图读取 .js 文件.
I defined that at the view and was trying to read the .js file.
所以,我要检查一下:将参数传递给 JavaScript 文件
谢谢!!
推荐答案
如果您将文件链接到模板文档正文末尾的脚本标记中并且文件正确加载(测试如果您的代码被执行,则为 console.log).
your code should be working, if you linked the file in a script tag at the end of your template's document body and the file is being loaded correctly (test with a console.log if your code is executed).
这篇关于将 window.SessionTimeout 脚本传递给 js 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!