Solved

option to return to page after timeout freeze

Posted on 2008-10-14
10
424 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
Comment Utility
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
Comment Utility
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
Comment Utility
You need to use a cookie
0
 
LVL 50

Expert Comment

by:Steve Bink
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

762 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

11 Experts available now in Live!

Get 1:1 Help Now