1

I´m doing an MVC app. And this is in my _Layout.cshtml

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() { ...

but i don´t know hot to use window.SessionTimeout in a js file.

The @PopupShowDelay is define in my view like this:

       @functions { 
        public int PopupShowDelay
        {
            get { return 60000 * (Session.Timeout - 1); }
        }
    }

UPDATE

Ok, i found the error. The problem is the variable

@PopupShowDelay

I defined that at the view and was trying to read the .js file.

So, i'm going tho check this: Passing parameters to JavaScript files

Thankls!!

Yogesh Patel
  • 818
  • 2
  • 12
  • 26
Erebo
  • 11
  • 1
  • 3

2 Answers2

0

You could have problems with this context in the nested function. You can read more here

NEOLPAR
  • 382
  • 1
  • 6
0

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).

321hendrik
  • 177
  • 1
  • 8