Solved

time limit across pages

Posted on 2013-06-03
4
337 Views
Last Modified: 2013-07-04
Hi

I need to program a task which is spread between several pages. I need to limit the time - such that after 150 seconds the user is sent to the end of the task.

I have created a session variable - "a1stimer" that starts when the user finished to read the instructions page. Afterwards, in each page I have introduced this little script:

<%stimer = timer
totime = stimer-session("a1stimer")
timeLeft = 150-cint(totime)

if cint(totime)>150 then
      response.redirect("end.asp")
end if%>

<script type="text/javascript">
var sec = <%=timeLeft%>;  
window.onload = countDown;

function countDown()
{
sec--;
if (sec == '00'){document.forms['form1'].submit();}
}
</script>


For some reason - the script is not working and the page not submit itself after 150 seconds. Any Ideas?
0
Comment
Question by:arielbf
  • 2
  • 2
4 Comments
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
Comment Utility
You eiher have to have your timer run in your vbscript or js script but not both.  It is best to run in js client side so it does not eat up your cpu which is what happens when you run a timer serverside with vbscript.

Remember, vbscript runs before the page loads and js runs after the page loads.  So if you are doing a page refresh you can set your timer by collecting the form variable easily in vb then placing it in js and let the js to the timing and redirecting.

Your code looks like it is using vb script serverside then sending to javascript client side and that is ok.  However, you can't send client side code to serverside unless you can figure out that space time continuum thing.
0
 

Accepted Solution

by:
arielbf earned 0 total points
Comment Utility
Well, I'm not sure why - but this version works:

<%stimer = timer
totime = stimer-session("a1stimer")
timeLeft = 150-cint(totime)

if cint(totime)>150 then
      response.redirect("end.asp")
end if%>

<script type="text/javascript">
var sec = <%=timeLeft%>;   // set the seconds
var min = 00;   // set the minutes
window.onload = countDown;

function countDown()
{
sec--;
if (sec == -01) {
   sec = 59;
   min = min - 1; }
  else {
   min = min; }

if (sec<=9) { sec = "0" + sec; }
time = sec + " seconds ";

if (document.getElementById) { document.getElementById('theTime').innerHTML = time; }
SD=window.setTimeout("countDown();", 1001);


if (min == '00' && sec == '00')
      {
      sec = "00";
      window.clearTimeout(SD);
      var t1=setTimeout("document.forms['form1'].submit();",1000);
      }
      
}
</script>


time Left:  <span id="theTime"></span>
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
Comment Utility
The reason it's working is because you have the js doing the work.

This line is created BEFORE the page loads
var sec = <%=timeLeft%>;   // set the seconds
Then it runs with the rest of the js AFTER the page loads.

The other possibility is there is some important code you are not showing that has nothing to do with the js code.

No where in your code sample do we see where you are setting time or session("a1stimer").  Without those being set, the response.redirect should not work.

I also don't see anyplace in your js code that should be redirecting.  I think by mistake or dumb luck those serverside variables were set and they were not before.
0
 

Author Closing Comment

by:arielbf
Comment Utility
This works
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 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

15 Experts available now in Live!

Get 1:1 Help Now