Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    automatically starting and stopping a script

    hi, i have the following script which counts up in 0.6 seconds as soon as the page loads.

    However i need this code to start automatically at 00.00am and restart at 23.59 every day.

    Is this possible, and if so could anyone help me? Thankyou.

    Code:
    <html>
    
    <head>
      <title></title>
    </head>
    
    <body onload="swdStart('beg1',0.6);">
    <span style="font-Size:20px;" >
    Number of pages printed today   
    </span>
    <br>
    
    <br>
    0000<input type="text" id="beg1" size="6" value="000000" onkeypress="return false;" >
    <input type="button" value="Start">
    <input type="button" value="Stop" onclick="swdStop('beg1');">
    <br>
    <br>
    
    <script language="JavaScript" type="text/javascript">
    <!--
    
    var swdCycleTime=50;
    
    
    // Functional Code
    
    var swd,swdObj,swdct,swdnow,swdcycle;
    var swdObjAry=new Array();
    
    function swdStart(id,ud){
     swdObj=document.getElementById(id);
     swdObj.value=0;
     swdnow=new Date();
     swdObj.now=swdnow.getTime();
     swdObj.ud=ud*1000;
     swdObj.d=0;
     if (!swdObj.c){
      swdObj.c=1;
      swdObjAry[swdObjAry.length]=swdObj;
     }
     if (swdObj.c==2){ swdObj.c=1; }
    }
    
    function swdCycle(){
     swdcycle=new Date().getTime();
     for (swd0=0;swd0<swdObjAry.length;swd0++){
      if (swdcycle>swdObjAry[swd0].now+swdObjAry[swd0].ud&&swdObjAry[swd0].c==1){
       swdObjAry[swd0].d++;
       swdObjAry[swd0].now=swdObjAry[swd0].now+swdObjAry[swd0].ud;
       swdObjAry[swd0].value=swdObjAry[swd0].d;
      }
     }
    }
    
    function swdStop(id){
     swdObj=document.getElementById(id);
     swdObj.c=2;
    }
    
    
    setInterval('swdCycle()',swdCycleTime);
    
    //-->
    </script>
    
    </body>
    
    </html>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,985
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    The code seems very complicated!

    I understand that you want the count to start at 0000 and increment every 0.6 seconds.

    Try this:-

    Code:
    <script type = "text/javascript">
    
    var secs;
    var timeInSecs;
    var ticker;
    var now = new Date();
    var a = now.getTime();
    var yy = now.getFullYear();
    var mm = now.getMonth();
    var dd = now.getDate();
    now = new Date(yy,mm,dd);
    var b = now.getTime();
    var diff = a -b;
    diff = parseInt(diff/1000/0.6);
    
    function startTimer(secs) {
    timeInSecs = parseInt(secs);
    ticker = setInterval("tick()",600); 
    }
    
    function tick(){
    var secs = timeInSecs;
    timeInSecs++;
    document.getElementById("countup").innerHTML = timeInSecs;
    
    }
    
    startTimer(diff);
    
    </script>
    
    <span id="countup" style="font-weight: bold;"></span>

    "The best horse won, and that's all you can ask in any sport". - Racing commentator

  • #3
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks for the quick reply, its greatly appreciated.

    Yes i need the count to start at 0000 and increment every 0.6 seconds. This seems to be working fine. All i need now is for the counter to restart every time it passes 0000. Il do some testing to see if i can get this working, but many thanks again for code you provided.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,985
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Quote Originally Posted by ss84 View Post
    All i need now is for the counter to restart every time it passes 0000.
    Well, I think you will find that is what happens. As the variable diff goes back to 0 at midnight when a new day starts.

    The highest displayed value will be 60*60*24/0.6 = 144000 (or actually 143999)
    Last edited by Philip M; 06-03-2009 at 01:19 PM.

  • Users who have thanked Philip M for this post:

    ss84 (06-03-2009)

  • #5
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Well, I think you will find that is what happens. As the variable diff goes back to 0 at midnight when a new day starts.
    if so, fantastic. thankyou very much

  • #6
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    finally, just out of interest..

    where in the script does it refer to 0000, so it lasts 24 hours?

  • #7
    Banned
    Join Date
    Mar 2009
    Posts
    248
    Thanks
    3
    Thanked 68 Times in 66 Posts
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>None</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">	
    
    	var GMToffset = -4;  // Your current GMT offset, whether Standard or Daylight
    
    	function init(){
    
    		var now = new Date();	
    		now.setHours(GMToffset + now.getHours() + now.getTimezoneOffset() / 60);	
    		var currTimeInSeconds = (now.getHours() * 3600) + (now.getMinutes() * 60) + now.getSeconds();
    		var timeUnits =  Math.round(currTimeInSeconds / .6).toString().replace(/(\d+)(\d{3})/,"$1,$2");
    		document.getElementById('counterDisplay').innerHTML = "Number of Pages Printed Today: " + timeUnits;
    		setTimeout("init()", 1000);
    	}
    
    	navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false);	
    
    </script>
    </head>
    	<body>
    		<div id="counterDisplay"></div>
    	</body>
    </html>

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,985
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Quote Originally Posted by ss84 View Post
    finally, just out of interest..

    where in the script does it refer to 0000, so it lasts 24 hours?
    I am afraid that I do not understand. The count starts at 0 at midnight and increases by 1 each 0.6 seconds. As I say, the highest vaue will shown will be 60*60*24/0.6 = 144000 (or actually 143999).

    12PackMack's script updates the counter every 1 second, while mine updates the counter every 0.6 seconds. So his script misses out some values as it jumps over. e.g. 65898 65900. And there also appears to be something wrong with his counter. By my calculation the value shown at 1600 GMT should be (60*60*16)/0.6 = 96000 which my script shows, but his shows a value around 66500.
    Last edited by Philip M; 06-03-2009 at 04:16 PM.

  • Users who have thanked Philip M for this post:

    ss84 (06-03-2009)

  • #9
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    I am afraid that I do not understand. The count starts at 0 at midnight and increases by 1 each 0.6 seconds. As I say, the highest vaue will shown will be 60*60*24/0.6 = 144000 (or actually 143999).

    12PackMack's script updates the counter every 1 second, while mine updates the counter every 0.6 seconds. So his script misses out some values as it jumps over. e.g. 65898 65900. And there also appears to be something wrong with his counter. By my calculation the value shown at 1600 GMT should be (60*60*16)/0.6 = 96000 which my script shows, but his shows a value around 66500.
    basically, the counter you kindly provided me seems to be exactly what i need, in which it counts up every 0.6 and restarts every 24 hours.

    however i am also trying to create another very similar which does the same but restarts every hour, therefore that was what my last question was relating to.

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,985
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Easy. Just change two lines, thus:-

    var a = now.getTime();
    var yy = now.getFullYear();
    var mm = now.getMonth();
    var dd = now.getDate();
    var hh= now.getHours();
    now = new Date(yy,mm,dd,hh);

    var b = now.getTime();


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •