Hi, I have problam whit jquery.
Whit IE work everything OK(work fine), but whit Mozila or Opera i have problam:
Hirs function "sec" work, fine, but secont in file index2.php "sec3" don't.

index.php

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script>
$(document).ready(function(){	
  var sec = setTimeout(function()
        {
        $('#sec').load('index2.php?id='+ Math.random());
        }, 1000);
});
</script>
</head>
<body>
<div align="center" id="sec">Please wait...</div>
<div align="center" id="sec2">Please wait...</div>
</body>
</html>

index2.php

<script type="text/javascript" src="jquery.js"></script>
<?php
echo "PHP code here...<br>";
?>
<script>
$(document).ready(function(){	
  var sec3 = setInterval(function()
        {
        $('#sec2').load(time.php?id='+ Math.random());
        }, 1000);
});
</script>

time.php

<?php
$sec = date("s");
echo $sec;
?>

Hi,
Use Firebug to find the problem. Its best way to debug JavaScript.

Hi, the $(document).ready() is executed when the DOM is ready to be used when loading a page. This is not valid when loading a page using AJAX.

So, the solution is using a callback after loading the first div:

$("#sec").load (url, callback);

your new code should look like this:

index.php

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script>
$(document).ready(function(){	
  var sec = setTimeout(function()
        {
        $('#sec').load('index2.php?id='+ Math.random(), function(){
             $('#sec2').load(time.php?id='+ Math.random());
         });
        }, 1000);
});
</script>
</head>
<body>
<div align="center" id="sec">Please wait...</div>
<div align="center" id="sec2">Please wait...</div>
</body>
</html>

.::AleX::.

This script works both under IE and Firefox:

function doRequest(){
	$('#sec').load('time.php?id='+ .random());
}

$(document).ready(
function(){
	var polling=new Polling();
polling.exec({handler:doRequest,onComplete:doRequest},1000,1);
}

);

The Polling class is a simple class from:
http://blog.viscent.info/?cat=3

Just do not use $(..).load, it will cause memory leakage!
Below code is more efficient:

function handleResponse(data){
	$('#s').html(data);
	task.interval=500;
	request();

}
function doRequest(){
	$.get('time.php?id='+ Math.random(),handleResponse);
}

var polling=new Polling();
var task={handler:doRequest,count:1,interval:1};
function request(){
	polling.exec(task);

}
$(document).ready(function(){
	request();
});
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.