7

below I have some JavaScript which listens for mouse activity and starts a timer if there is no activity and then redirects the user to another page after say 2 minutes..The problem is that if I have this in a layout page it works fine but when I put it in the content editor part in sharepoint the timer works but the page does not redirect..Can someone please tell me what's wrong.

<script type="text/javascript">

    var mins, secs, TimerRunning, TimerID;
    TimerRunning = false;

    var activity;
    document.documentElement.onmousemove = function () {
        clearInterval(activity);
         activity = Init();
       // activity = setInterval(saySomething, 5000);
    }

    function Init() //call the Init function when u need to start the timer
    {
        mins = 2;
        secs = 0;
        StopTimer();
        StartTimer();
    }

    function StopTimer() {
        if (TimerRunning)
            clearTimeout(TimerID);
        TimerRunning = false;
    }

    function StartTimer() {
        TimerRunning = true;
        window.status = "if no activity is detected you will be logged out in " + Pad(mins) + ":" + Pad(secs);
        TimerID = self.setTimeout("StartTimer()", 1000);

        Check();

        if (mins == 0 && secs == 0)
            StopTimer();

        if (secs == 0) {
            mins--;
            secs = 60;
        }
        secs--;


    }

    function Check() {
        if (mins == 1 && secs == 0)

        // alert("You have only 2 minutes remaining");
        if (mins == 0 && secs == 0) {

            window.location = "Http://Test";

        }
    }

    function Pad(number) //pads the mins/secs with a 0 if its less than 10
    {
        if (number < 10)
            number = number;
        return number;
    }




    } </script>
  • 2
    try window.location.href? That always worked for me. – David Lozzi Feb 21 '12 at 4:04
  • I replaced windows.location with windows.location.href but it is not working..the timer stops but it does not redirect – steve Feb 21 '12 at 4:25
  • 3
    did you place an alert message inside the if loop and check whether the condition is being called, just before the window.location ? – Deepu Nair Feb 21 '12 at 4:48
  • Yea it wasn't getting inside..forgot to add Curly brackets after first if statement..its workig now....Thanks for helping me. – steve Feb 21 '12 at 5:06
  • 2
    Try to show alert box using this way: setTimeout(function(){alert("hi");}, 0); – Sergei Sergeev Feb 21 '12 at 8:45
1

Well, although this is a trick, there is one more thing you can try. Well there is not activity on page for 2 minute, then make the target page link visible on the page and trigger the click action on that link in the page.

For this purpose you can have a look at jQuery trigger function.

0

Good to know that your issue is solved by adding curly brackets for your if loop.

I believe the setTimeout function commented by Kai would only fire the alert waiting for a particular interval specified. I think thats not what you are intending to do.

You can think about alerting the user before redirecting, by displaying a label text. The label text can be hidden using a div tag and be displayed just within your logic whenever required.

Good Luck Steve.

0

You can try this one,

window.open("http://www.w3schools.com");//opens in new tab

reference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.