Pain Posted April 3, 2012 Share Posted April 3, 2012 Hi there. Im pretty new in AJAX and i've learned some basics from w3schools so i can now confidently load info from the database to the web page without actually reloading it. What i want to learn is how to load info from the database while refreshing it every ten seconds or so. Now i know there are plenty of examples of how to do this, but they all seem different and it really confuses a newbie like me. Here is my basic AJAX script: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Home</title> <script type="text/javascript"> function loadXML() { var xmlhttp; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest; } xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("myDiv").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","users.php",true); xmlhttp.send(); } </script> </head> <body> <div id="myDiv">Get the info!</div> <button type="button" onClick="loadXML()">Greet</button> </body> </html> If somebody could guide me all the way... it would be fantastic! I don't want to just copy a line or two but I rather want to understand it. So far i figured that i'd have to use a timer for this matter..? Quote Link to comment https://forums.phpfreaks.com/topic/260302-ajax-mysql-query-every-10-seconds/ Share on other sites More sharing options...
kicken Posted April 3, 2012 Share Posted April 3, 2012 Use setInterval to define a function that will execute your code every 10 seconds. setInterval(function(){ loadXML(); }, 10000); Quote Link to comment https://forums.phpfreaks.com/topic/260302-ajax-mysql-query-every-10-seconds/#findComment-1334184 Share on other sites More sharing options...
Pain Posted April 4, 2012 Author Share Posted April 4, 2012 Omg never imagined this would be so easy! Thanks! God i'm SO making a donation to phpfreaks after i fully master ajax! Quote Link to comment https://forums.phpfreaks.com/topic/260302-ajax-mysql-query-every-10-seconds/#findComment-1334190 Share on other sites More sharing options...
Pain Posted April 4, 2012 Author Share Posted April 4, 2012 Oh forgot to ask one thing. If i were to refresh the page every second instead of ten seconds, would there be a downside to it? Quote Link to comment https://forums.phpfreaks.com/topic/260302-ajax-mysql-query-every-10-seconds/#findComment-1334391 Share on other sites More sharing options...
UrbanDweller Posted April 5, 2012 Share Posted April 5, 2012 Surely it will slow ur site down reqesting so many queries a second Quote Link to comment https://forums.phpfreaks.com/topic/260302-ajax-mysql-query-every-10-seconds/#findComment-1334529 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.