date display 'sticks'

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
inosent1
Forum Commoner
Posts: 97
Joined: Wed Jan 28, 2009 12:18 pm

date display 'sticks'

Post by inosent1 »

I use the following ajax code to call another php page, which has the effect of 'refreshing' w/o refreshing. the idea is to be a seamless refresh, etc


Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Time</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/JavaScript">
var xmlHttp;
 
function getDiscussion(str)
{
    var url="test.php";
    xmlHttp=GetXmlHttpObject(stateChanged)
    xmlHttp.open("GET", url , true)
    xmlHttp.send(null)
}
 
function stateChanged()
{
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
        document.getElementById("myAJAXdiv").innerHTML=xmlHttp.responseText;
    }
}
 
function GetXmlHttpObject(handler)
{
    var objXmlHttp=null
 
    if (navigator.userAgent.indexOf("Opera")>=0)
    {
        alert("Opera not supported...")
        return;
    }
    if (navigator.userAgent.indexOf("MSIE")>=0)
    {
        var strName="Msxml2.XMLHTTP"
        if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
        {
            strName="Microsoft.XMLHTTP"
        }
        try
        {
            objXmlHttp=new ActiveXObject(strName)
            objXmlHttp.onreadystatechange=handler
            return objXmlHttp
        }
        catch(e)
        {
            alert("Error. Scripting for ActiveX might be disabled")
            return
        }
    }
    if (navigator.userAgent.indexOf("Mozilla")>=0)
    {
        objXmlHttp=new XMLHttpRequest()
        objXmlHttp.onload=handler
        objXmlHttp.onerror=handler
        return objXmlHttp
    }
} 

function startWatching()
{
 getDiscussion(this.value);
 setTimeout("startWatching()",2000);
}
</script>
</head>

<body onLoad="startWatching();">
	<div style="margin: 10px;" id="myAJAXdiv"></div>
</body>
</html>




the code of 'test.php' includes this

Code: Select all

$timea = date("is", time()); 
echo $timea;


As you can see in the ajax code, the time interval for the 'refresh' is shown as 2000, or 2 seconds. when you load the code you will see, *generally* that the time changes every 2 seconds.

however, sometimes it will 'freeze up' and hang for about 5-8 seconds before showing the next time value. for my purposes this is inconvenient. i need a reliable clock that seamlessly re-displays every two seconds, w/o 'sticking'
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: date display 'sticks'

Post by twinedev »

You will probably get a better answer posting this in the correct section instead of PHP Code.
maxx99
Forum Contributor
Posts: 142
Joined: Mon Nov 21, 2011 3:40 am

Re: date display 'sticks'

Post by maxx99 »

Yep its Javascript.... anyway.... why do you need AJAX requests to get time!?
http://www.w3schools.com/js/js_obj_date.asp

If you don't want rely on client time, you can can sync with server at start and every 5 mins or so (with e.g. ajax request).
Post Reply