Solved

option to return to page after timeout freeze

Posted on 2008-10-14
10
430 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

777 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