Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

time limit across pages

Posted on 2013-06-03
4
Medium Priority
?
351 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 54

Expert Comment

by:Scott Fell, EE MVE
ID: 39216721
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
ID: 39231538
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 54

Expert Comment

by:Scott Fell, EE MVE
ID: 39231584
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
ID: 39299428
This works
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
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…
Suggested Courses

916 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