Link to home
Start Free TrialLog in
Avatar of Erikal
Erikal

asked on

option to return to page after timeout freeze

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)">
Avatar of Erikal
Erikal

ASKER

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.
Avatar of Erikal

ASKER

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.
Avatar of Michel Plungjan
You need to use a cookie
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.
Avatar of Erikal

ASKER

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?
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


Avatar of Erikal

ASKER

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

Avatar of Erikal

ASKER

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)">
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Erikal

ASKER

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.