Solved

option to return to page after timeout freeze

Posted on 2008-10-14
10
427 Views
Last Modified: 2013-12-13
Hello,
I have some test pages (.php) with javascript timeout functions. In each test page, the user is given two options:
1- continue with the test until timeout, when the form fields are disabled,
2- Click the submit button to leave the test page before timeout and submit form fields.

The time out in this case is 15 minutes. If the user leaves the page too early, say clicks the submit button  4 minutes before the 15 minutes runs out, I want to give him/her the option of returning to the page to continue answering/editing questions (i.e to be able to use the remaining 4 minutes). In other words: can I freeze the timeout function at submit and, if user chooses to return, restart the time count from where left off and continue untill the full 15 minutes expires.

Here are the javascript codes:
Timeout function:
=================================
<!DOCTYPE html PUBLIC>
<html lang="en">
<head>
<title>English Test</title>

<script type="text/javascript">
      window.onload=init;
      function init(){
            var minutes=(15);
            setTimeout("nextPage()", 1000*60*minutes );
      }
      function nextPage(){
            //assuming you have <form id="English1"...>
            var f = document.getElementById("English1");
            for( var i=0; i < f.elements.length; ++i)
            {
                    if( String(f.elements[i].type).toLowerCase() != "submit" )
                  f.elements[i].disabled=true;
            }
      }
       function activate(f){
            for( var i=0; i < f.elements.length; ++i)
            {
                  f.elements[i].disabled=false;
            }
      return true;
      }
</script>
==============================
in the body tag:
==============================
<body text="#000000" style="background: #e5e5e5; text-align:center; height:2500px;" onload="display();">
=============================
and in the form action tag:
==============================
<form ID="English1" action="<? echo $_SERVER['PHP_SELF']; ?>" method="post" onsubmit="return activate(this)">
0
Comment
Question by:Erikal
  • 6
  • 3
10 Comments
 

Author Comment

by:Erikal
ID: 22718747
I forgot to add,
the script was kindly suggested by hielo and working perfectly.

When the time out is paused after submit button is clicked and the user is given the chance to go back and use the remaining time to check the answers, the submit command should not be activated, unless the user decides to reject the 'go back' option t (i.e. decides not to go back to the test page from the alert message, but to continue to the next page). If the user chooses to go back to the current test, the submit command should then only be activated if:
1- timeout occurs (whole 15 minutes)
2- User again clicks the submit button and there are now less than say, 4 minutes remaining for the test.
0
 

Author Comment

by:Erikal
ID: 22719117
Further clarification:
The javascript scipt given in the question above works fine.
But, I need to modify the script so that the timeout event can  be paused and resumed depending on user's choice.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 22724342
You need to use a cookie
0
 
LVL 50

Expert Comment

by:Steve Bink
ID: 22726427
I wouldn't use a cookie since those can be manipulated by the user.  A session variable would work just as well.  Have your test page check for the existence of the session time.  If it exists, then that is how much time is left.  If it does not exist, then set it to 15 minutes (or 900 seconds, if you want that kind of accuracy).  If the test is finished, or if the time runs out, be sure to unset the cookie, and use a flag on the test to indicate it was completed.
0
 

Author Comment

by:Erikal
ID: 22726583
Thank you routinet,
I think I did not explain what I need to do correctly. It is rather difficult for a beginner even to set the question properly.
I already have a timeout javascript script on the page (see code above), which disables the form input at timeout and alerts the student that the 15 minutes is up. Then the student clicks the button to submit the form. Whiich in turn displays another alert and redirects to next test page.
The user also has the option anytime to click the submit button, whether or not timeout is reached.

All I am looking to do is to modify the script, so that if the user clicks the submit button prematurely, either by mistake or intentionaly, I want to give them the chance to go back to the page and continue.

In other words, on the first occasion of submit button being clicked, unless timeout is already reached, I want the form submission to be put on hold and pause the timer so that the user returns to the page and resumes the test where left off.

Sorry about this long winded explanation.

Any ideas?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 22728373
Session variable is also cookie related.

A timer can also be manipulated on the client.

Since you just want to allow the user to return, use the php.
Set the time in the php and use post in the form. Then if they click back, the form is reposted and you can check the time in the session and refill the fields on the php side if there is still time. You can use the time saved in the session to see if they manipulated the time - in fact you should


0
 

Author Comment

by:Erikal
ID: 22728797
Hi again,
I am wondering; can I just add a confirm box  in the countdown script to give the user the choice to submit or not (and to stay on the page or not of course)? If the user chooses cancel, the form will not be submitted and the user will stay on the page to continue until timeout.

When the confirm box appears would that mean the user already left the page or can they simply click cancel an continue working on the page.
I beleive I need to add something like - onsubmit="return confirm_something() in the form action field.

If you think this will work where and how would I insert the confiem function in this timeout script
===================
<script type="text/javascript">
      window.onload=init;
      function init(){
            var minutes=(15);
            setTimeout("nextPage()", 1000*60*minutes );
      }
      function nextPage(){
            //assuming you have <form id="English1"...>
            var f = document.getElementById("English1");
            for( var i=0; i < f.elements.length; ++i)
            {
                    if( String(f.elements[i].type).toLowerCase() != "submit" )
                  f.elements[i].disabled=true;
            }
      }
       function activate(f){
            for( var i=0; i < f.elements.length; ++i)
            {
                  f.elements[i].disabled=false;
            }
      return true;
      }
</script>


Do you think this will work.
Also if this is possible, would this mean the countdown will start from scratch

0
 

Author Comment

by:Erikal
ID: 22728854
Just one more thing.
I already have the 'onsubmit="return activate(this)' command in the form action (see below). Would the 'onsubmit="return confirm_something() ' command clash with this?

<form ID="Maths1" action="<? echo $_SERVER['PHP_SELF']; ?>" method="post" onsubmit="return activate(this)">
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 22728923
Just add it

function activate(f){

  if (timeLeft > 0 && confirm('You still have some time, do you want to stay?')) return false;

  for( var i=0; i < f.elements.length; ++i) {

    f.elements[i].disabled=false;

  }

  return true;

}

 

and have

var minutes = 15;

var timeLeft = minutes*60*1000;

var tId = "";

var leave = "";

function countdown() { timeLeft -= 1000; }

window.onload=function() {

  leave = setTimeout("nextPage()", timeLeft);

  tId = setInterval('countdown()',1000)

}

 

instead of

 

window.onload=init;

  function init(){

            var minutes=(15);

            setTimeout("nextPage()", 1000*60*minutes );

      }

Open in new window

0
 

Author Closing Comment

by:Erikal
ID: 31506197
Thanks mplungjan,
That works great.
I suppose, asking the right question is more than half way to getting the right answer and doing it on the first attempt is even better all round.
Thank you very much for your patience.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now