Save as test.html
<html>
<head>
<script src="javascript/jquery-1.7.min.js" type="text/javascript"></script>
<style>
.restart, .stop, .clear
{
font-size: 20px;
margin-right: 20px;
}
.container
{
position: relative; border: 1px black solid; width: 500px; height: 300px;
}
.code
{
margin-top: 30px; position: relative; border: 1px black solid; width: 800px; height: 200px;
}
</style>
<script>
window.onload = function()
{
t = setInterval("ajax('/test2.php', 'xhr=blahblah', 'GET', 'script', 10000, true)", $('.intsec').val() * 1000);
};
function timeout()
{
alert("Operations has timed out. \n\nPlease try again.");
}
function success(data, textStatus, XMLHttpRequest)
{
console.log(data + textStatus + " " + XMLHttpRequest);
// console.log(JSON.stringify(data));
$('.code').html(data);
return false;
}
function catch_error(jqXHR, textStatus, errorThrown)
{
if(textStatus == 'timeout') timeout();
console.log(jqXHR);
console.log(errorThrown);
console.log(textStatus);
// console.log(JSON.stringify(jqXHR));
return false;
}
function ajax(url, data, type, dataType, timeout, async)
{
$.ajax({
dataType: dataType,
type: type,
url: url,
data: data,
async: async,
timeout: timeout,
success: success,
error: catch_error
});
return false;
}
$('.restart').live('click', function()
{
t = setInterval("ajax('/test2.php', 'xhr=blahblah', 'GET', 'script', 10000, true)", $('.intsec').val() * 1000);
$(this).removeClass('restart').addClass('stop').text('Stop');
});
$('.stop').live('click', function()
{
clearInterval(t);
$(this).removeClass('stop').addClass('restart').text('Re-Start');
});
$('.clear').live('click', function()
{
$('.container').empty();
});
$('.intsec').live('change', function()
{
if($('.stop').length)
{
clearInterval(t);
t = setInterval("ajax('/test2.php', 'xhr=blahblah', 'GET', 'script', 10000, true)", $('.intsec').val() * 1000);
}
});
</script>
</head>
<body>
<a class='stop' href='#'>Stop</a>
<a class='clear' href='#'>Clear Box</a>
Interval in seconds: <input class='intsec' value='5' />
<div class='container'></div>
<textarea class='code'></div>
</body>
</html>
Save as test2.php
<?php
$top = mt_rand(0,290);
$left = mt_rand(0,490);
$hue = mt_rand(0,100);
$sat = mt_rand(0,100).'%';
$light = mt_rand(0,100).'%';
$class = $top . $left;
$background = "hsl($hue, $sat, $light)";
$code = "$('.$class').remove();";
$code .= "\n$('.container').append(\"<div class='$class' style='position: absolute; top:$top; left:$left; width: 10px; height: 10px; background-color: $background;'></div>\");";
echo $code;
$code = str_replace('$', '\$', $code);
?>
This may help you with getting started with jquery.
It will allow you to send jQuery/Javascript code back to the browser for processing. This will allow you to control more of the functionality on the server side.
There is an interval timer that starts on window.onload. It will receive the default value of 5 seconds from the initial page.
If you decide to return data in a different format ie., json, xml, html, etc. Google jquery ajax and visit the jquery site for additional information on ajax.
As you can see, jquery ajax is very concise. However, there are different options that you may want to play around with.
You will also need to download the jquery framework.
The console.log statements will display the results of the call in your browser's console log. Comment out some or all of these statements when you do not need them.
test.html: Browser side (requesting)
test2.php: Server side (sending)