Question

So I've been working on this clock all day and finally got it to work flawlessly on my sandbox page.

With the sandbox page having close to nothing on it, the clock runs beautifully, but when I add the same code to my clock on my index.php, it comes up choppy and flickers between the time and "NaN:NaN:NaN:am".

So what I was wondering is if the more content I have on a page, will it cause it to "flicker"?

<script language="javascript" type="text/javascript">

function clock(i){
var i =new Number(i);//Establish var type so math adds up
i=(i+1000);//Add a second to timestamp
setTimeout('clock("'+i+'")',1000)//Setup timer

var lastTick=new Date(i);//New timestamp applied to date function
var h=lastTick.getHours();
var m=lastTick.getMinutes();
var s=lastTick.getSeconds();
var diem="am";

//Some clean up to make it "clock-like"
if(m<10){m="0"+m;}
if(s<10){s="0"+s;}
if(h==0){h=12;}
if(h>12){h=h-12;diem="pm";}

//Setup string for clock and insert it to div
var clock=h+":"+m+":"+s+" "+diem;
$("#clock").text(clock);
}
</script>

<div id="clock"></div>
<script type="text/javascript">
    clock('<?php echo (time()*1000)?>');//Convert PHP timestamp to Javascript
</script>

Im getting this across multiple platforms. When I increase the repetition rate (lower setTimeout rate to 100ms) it looks better but then it eventually errs and stays in the flawed state.

My ultimate goal is to have a clock display server time. I figured using ajax to ping to the server for the time would just bog things down, so I thought a timer started off a PHP timestamp would save bandwidth. If there's a more efficient way to go about this, I'd like to know.

Was it helpful?

Solution

No php is not influenced by JavaScript

this code might be interfering with other javascript code you have written check if you didn't define same variable twice

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top