time limit across pages

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?
arielbfAsked:
Who is Participating?
 
arielbfAuthor Commented:
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
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
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
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
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
 
arielbfAuthor Commented:
This works
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.

All Courses

From novice to tech pro — start learning today.