473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Javascript timer issue

Ok I'm working on a timer that will work over multiple pages. Basically what this means is I need the timer to start on page one and give the user x amount of time to complete a quiz that spans several pages. At the end unless they completed the quiz it then pops up a message stating that they took to long to complete the quiz. I already have a timing function working but the issue is when you go to the next page it obviously stops running. I've tried developing a cookie to store the value for transfer between pages but I"m not sure how pull it back out,continue processing the time then store the adjusted time back in for retrieval later. Has anyone here tried to do something similar to this before if so how did you get it functioning?
Nov 28 '05 #1
3 12326
Niheel
2,460 Expert Mod 2GB
What you can do is when the user starts or arrives at a page, set a cookie with the start time and end time.

Example;
Endtime = starttime + alloted seconds;

Then all you have to do is have each page load up js code that checks the current time with the Endtime. If the current time is greater than the end time, it serves a popup.

The only issue i can think of is, if the individual keeps changing the system time, it might alter the amount of time available.

If this is the case you might want to store your sessions in a database on the server side, then via js call a php file every second that would check if it's timesup, if so it would send out a popup.
Nov 29 '05 #2
i've got a good one for you, put this in a globalfunction page that your quiz page refers to, here's a good timer code that the actual time is selected by the user...It puts the time in the status bar

Expand|Select|Wrap|Line Numbers
  1.  function updatetimeleft() 
  2. {
  3. timeleft = var timelimit = cbotimelimit.options[cbotimelimit.selectedIndex].value;// Here is the selected timelimit
  4.     timeleft--;
  5.  
  6.     if (timeleft == 0)
  7.     {
  8.         alert("Time is Up")
  9.         stoptimer(); //My Own function to stop the timer
  10.  
  11.     }
  12.     else
  13.     {
  14.         var minutes = Math.floor(timeleft/60)
  15.         var seconds = Math.floor(timeleft - (60 * minutes))
  16.         if (minutes < 10)
  17.             {
  18.                 minutes = "0" + minutes;
  19.             }
  20.         if (seconds < 10)
  21.             {
  22.                 seconds = "0" + seconds;
  23.             }
  24.         window.status = "Time Left is " + minutes + ":" + seconds;
  25.     }
  26. }
  27.  
  28.  
  29. //For the function that initiates the quiz, put in this statement
  30.  
  31.     if (timeleft == -1)
  32.     {
  33.         window.status = "No Time Limit";
  34.     }
  35.     else
  36.     {
  37.         quiztimerid = window.setInterval("updatetimeleft()",1000);
  38.     }
  39.  
Hopefully this helps a little bit
Dec 26 '05 #3
MrWSD
1
Expand|Select|Wrap|Line Numbers
  1.  #txt { 
  2. border:none;
  3. font-family:verdana;
  4. font-size:16pt;
  5. font-weight:bold;
  6. border-right-color:#FFFFFF
  7. }
  8.  
  9.  
  10. var mins
  11. var secs;
  12.  
  13. function cd() {
  14.     mins = 1 * m("10"); // change minutes here
  15.     secs = 0 + s(":01"); // change seconds here (always add an additional second to your total)
  16.     redo();
  17. }
  18.  
  19. function m(obj) {
  20.     for(var i = 0; i < obj.length; i++) {
  21.         if(obj.substring(i, i + 1) == ":")
  22.         break;
  23.     }
  24.     return(obj.substring(0, i));
  25. }
  26.  
  27. function s(obj) {
  28.     for(var i = 0; i < obj.length; i++) {
  29.         if(obj.substring(i, i + 1) == ":")
  30.         break;
  31.     }
  32.     return(obj.substring(i + 1, obj.length));
  33. }
  34.  
  35. function dis(mins,secs) {
  36.     var disp;
  37.     if(mins <= 9) {
  38.         disp = " 0";
  39.     } else {
  40.         disp = " ";
  41.     }
  42.     disp += mins + ":";
  43.     if(secs <= 9) {
  44.         disp += "0" + secs;
  45.     } else {
  46.         disp += secs;
  47.     }
  48.     return(disp);
  49. }
  50.  
  51. function redo() {
  52.     secs--;
  53.     if(secs == -1) {
  54.         secs = 59;
  55.         mins--;
  56.     }
  57.     document.cd.disp.value = dis(mins,secs); // setup additional displays here.
  58.     if((mins == 0) && (secs == 0)) {
  59.         window.alert("Time is up. Press OK to continue."); // change timeout message as required
  60.         // window.location = "yourpage.htm" // redirects to specified page once timer ends and ok button is pressed
  61.     } else {
  62.         cd = setTimeout("redo()",1000);
  63.     }
  64. }
  65.  
  66. function init() {
  67. cd();
  68. }
  69. window.onload = init;
  70.  
  71.  

[HTML]
<!-- Paste this code into the HEAD section of your HTML document.
You may need to change the path of the file. -->

<script type="text/javascript" src="countDown.js"></script>



<!-- Paste this code into the BODY section of your HTML document -->

<form name="cd">
<input id="txt" readonly="true" type="text" value="10:00" border="0" name="disp">
</form>
[/HTML]
Aug 28 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Alex Fitzpatrick | last post by:
Just by way of introduction, I'm currently the principal developer and maintainer of the a JavaScript editor plug-in for Eclipse. https://sourceforge.net/projects/jseditor/ The plug-in as it...
5
by: Dhilip Kumar | last post by:
Hi all, I have developed a windows service using the windows service project template in VS.NET. I have used three controls in the service, a timer, performance counter and a message queue...
7
by: C.Joseph Drayton | last post by:
I have a problem that I am hoping someone can help me with. First let me describe the problem. I have an HTML form that in one field has an onBlur call to a JavaScript function. When you exit the...
4
by: E | last post by:
I am having trouble with setTimeout working on a second call to the setTimeout function from a second page which is an html page. Here is the scenario. I have a web page and onload it calls a...
10
by: jason_box | last post by:
Hello, I was wondering if there was a way to have a javacript be activated by an input button that would call to a cgi program and querey every 10minutes and the cgi would update the page without...
3
by: anagram | last post by:
hi , i am working on an online quiz.i have prepard the database access and everything in php.i want to know how to implement a timer in javascript such that once the time is up for answering...
10
by: igor | last post by:
I have recently discovered that the system.Timers.Timer from.Net Framework v1.1 is not reliable when used on Windows 2003 server. When incorporated into a Windows Service, the timer_elapsed event...
2
by: Surya Vellanki | last post by:
Hi all, I have been struggling with this problem from a few days. I hope I will get some help here. I am developing an online quiz portal with ASP.NET 2.0 and I am using Atlas framework for...
8
by: Ollie Riches | last post by:
I'm looking into a production issue related to a windows service and System.Timers.Timer. The background is the windows service uses a System.Timers.Timer to periodically poll a directory location...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.