...

View Full Version : automatically starting and stopping a script



ss84
06-03-2009, 12:11 PM
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.


<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>

Philip M
06-03-2009, 01:06 PM
The code seems very complicated!

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

Try this:-


<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

ss84
06-03-2009, 01:49 PM
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. :)

Philip M
06-03-2009, 02:15 PM
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)

ss84
06-03-2009, 02:17 PM
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 :thumbsup:

ss84
06-03-2009, 02:30 PM
finally, just out of interest..

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

12 Pack Mack
06-03-2009, 04:47 PM
<!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>

Philip M
06-03-2009, 04:51 PM
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.

ss84
06-03-2009, 06:12 PM
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.

Philip M
06-03-2009, 06:44 PM
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();



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum