Oto mój kod:
<script type="text/javascript"> var xmlHttp function GetXmlHttpObject(){ var objXMLHttp = null; if(window.XMLHttpRequest){ try{ objXMLHttp = new XMLHttpRequest(); }catch (e){ objXMLHttp = false; } }else if(window.createRequest){ try{ objXMLHttp = new window.createRequest(); }catch (e){ objXMLHttp = false; } }else if(window.ActiveXObject){ try{ objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP"); }catch (e){ try{ objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP"); }catch (e){ objXMLHttp = false; } } } return objXMLHttp; } function GetShouts() { xmlHttp = GetXmlHttpObject() if (xmlHttp == null) { alert ("Browser does not support HTTP Request") return } var url="zal_art.php" //The file you're using to display the entries/shouts most likely the current file.. xmlHttp.open("GET", url, true) xmlHttp.onreadystatechange = function () { if (xmlHttp.readyState == 4) { if (xmlHttp.status == 200) { document.getElementById("zal_art").innerHTML = xmlHttp.responseText; //Shoutarea is the id of my div the shouts appear in so change that accordingly. } } }; xmlHttp.send(null); } GetShouts(); setInterval("GetShouts()", 1000); function saveData() { xmlHttp = GetXmlHttpObject() if (xmlHttp == null) { alert ("Browser does not support HTTP Request") return } xmlHttp.open('POST', 'zal_art.php'); xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xmlHttp.send('&name=' + document.shout.name.value + '&shout=' + document.shout.message.value); //document.shout.name.value is the field the user would enter their name there's also one for the message field do this for every input you have. document.shout.message.value = ''; document.shout.message.focus(); xmlHttp.overrideMimeType('text/html; charset=ISO-8859-2'); xmlHttpRequest.overrideMimeType('text/html; charset=ISO-8859-2'); } </script>
Problem jest z linijką:
setInterval("GetShouts()", 1000);
Ma ktoś pomysł, jak przerobić skrypt, żeby w zależności od tego czy są rekordy w bazie danych, czy ich nie ma, odświeżał się lub nie (lub odświeżał się co inną ilość czasu).