Sunday 29 April 2012

Clock Stopwatch Timeout event



  1. Link the external javascript file into header
    <head>
    <script type="text/javascript" src="js/clock.js"></script>
    </head>
  2. load the function getSecs() from javascript file using setTimeout
    <body onload="window.setTimeout('getSecs()',1)">
  3. copy the following code into js/clock.js file:
    1.     
    2.     <!-- Begin
    3.     startday = new Date();
    4.     clockStart = startday.getTime();
    5.     
    6.     function initStopwatch() 
    7.     {
    8.         var myTime = new Date();
    9.         return((myTime.getTime() - clockStart)/1000);
    10.     }
    11.     function getSecs() 
    12.     {
    13.         var tSecs = Math.round(initStopwatch());
    14.         var iSecs = tSecs % 60;
    15.         var iMins = Math.round((tSecs-30)/60);
    16.         var sSecs ="" + ((iSecs > 9) ? iSecs :  + iSecs);
    17.         var sMins ="" + ((iMins > 9) ? iMins :  + iMins);
    18.         document.getElementById("timespent").value = sMins+":"+sSecs;
    19.         var duration = QueryString("Duration");
    20.         if(sMins>=duration)
    21.             window.location="http://localhost/eLearning/examNotice.php";
    22.         window.setTimeout('getSecs()',1000);
    23.         QueryString("CourseID");
    24.     }
    25.     // End -->
    26.     
    27.     function QueryString(key) 
    28.     {  
    29.         //Get the full querystring  
    30.         fullQs = window.location.search.substring(1);  
    31.         //Break it down into an array of name-value pairs  
    32.         qsParamsArray = fullQs.split("&");  
    33.         //Loop through each name-value pair and   
    34.         //return value in there is a match for the given key  
    35.         for (i=0;i<qsParamsArray.length;i++) {  
    36.         strKey = qsParamsArray[i].split("=");  
    37.             if (strKey[0] == key) {  
    38.                 return strKey[1];  
    39.             }  
    40.         }  
    41.     }
    42.      

No comments:

Post a Comment