I made a counter that counts from one to infinity and if I refresh the page it doesn’t stop.
However I have used ajax to update some datas in the database and this isn’t working well …
I think the problem is thet the ajax file dosn’t run
HTML :
<html>
<head>
<?php include "config.php"; ?>
<script type="text/javascript">
function createXMLHttpRequest(){
var ua;
if(window.XMLHttpRequest) {
try {
ua = new XMLHttpRequest();
}catch(e) {
ua = false;
}
}else if(window.ActiveXObject) {
try{
ua = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e) {
ua = false;
}}
return ua;}
var some = createXMLHttpRequest(c);
function some1(){
some.open('GET', 'Untitled_4.php?value='+c);
some.onreadystatechange = hand;
}
function hand() {
if(some.readyState == 4)
{
some.send(null);
}
}
var c=
<?php
$c= mysql_fetch_array( mysql_query("SELECT * FROM `clock` WHERE `id`=1"));
$l=time()-$c['time'];
$a=$l/1;
$a=floor($a);
$a=$a+$c['value'];
echo $a;
?>;
var t;
var timer_is_on=0;
function timedCount()
{
document.getElementById('txt').innerHTML=c;
c=c+1;
t=setTimeout("timedCount()",1000);
}
function doTimer()
{
if (!timer_is_on)
{
timer_is_on=1;
timedCount();
}
}
</script>
</head>
<body onunload="some1(c)" onload="doTimer()">
<form>
<div id="txt" ></div>
</form>
</body>
</html>
AJAX File :
<?php
include "config.php";
$value=$_GET['value'];
mysql_query("UPDATE `clock` SET `time`=".time().",`value`= $value WHERE `id` = 1 ");
echo "1";
?>
The
readystatewon’t update unless you send theXMLHttpRequest.The following version of
some1should fix your problem:You don’t really need the
onreadystatechangedhandler because you send the request when the page unloads and there won’t be enough time to handle a change of the XMLHttpRequest’s state I guess.