Solved

time limit across pages

Posted on 2013-06-03
4
338 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
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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SharePoint 2013 Blog Template 2 31
jQuery Syntax 2 47
Script Extension require update AD computer location base on sites subnet 5 28
Message not shown 5 29
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

932 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

13 Experts available now in Live!

Get 1:1 Help Now