Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 453
  • Last Modified:

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)">
0
Erikal
Asked:
Erikal
  • 6
  • 3
1 Solution
 
ErikalAuthor Commented:
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
 
ErikalAuthor Commented:
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
 
Michel PlungjanIT ExpertCommented:
You need to use a cookie
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
Steve BinkCommented:
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
 
ErikalAuthor Commented:
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
 
Michel PlungjanIT ExpertCommented:
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
 
ErikalAuthor Commented:
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
 
ErikalAuthor Commented:
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
 
Michel PlungjanIT ExpertCommented:
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
 
ErikalAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now