I want to be able to refresh the page every 2 minutes so that the PHP file is then run so it will repopulate a table of data. I have read that setTimeout() can be used to do this but I can't figure out how to do this. Anyone have any example code?
<script type="text/javascript">
function showSellers(isbn)
{
//if there is no isbn given, show nothing and return nothing
if (isbn=="")
{
document.getElementById("sellers").innerHTML="";
return;
}
//AJAX request for IE7+, Firefox, Chrome, Opera, Safari
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
//AJAX request for IE6, IE5
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
//if the state of the page changes, do this
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("sellers").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax_seller.php?isbn="+isbn,true);
xmlhttp.send();
}
</script>
If you want to do it periodically you can use setInterval;
setInterval(function(){ showSellers("my_isbn"); }, 120000);
You can use a global variable for the isbn;
globalISBN = "123456";
setInterval(function(){ showSellers(globalISBN); }, 120000);
Retrieving the GET parameter;
function getParameter(name)
{
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
var hash = hashes[i].split('=');
if(hash[0] == name)
{
return hash[1];
}
}
return null;
}
setInterval(function(){ showSellers(getParameter("isbn")); }, 120000);