javascript - Refreshing my php page with AJAX every 5 seconds - Stack Overflow

I'm creating a link-sharing website and on my index.php page (the page I want to refresh every 5 s

I'm creating a link-sharing website and on my index.php page (the page I want to refresh every 5 seconds) there are posts/links that must appear automatically (AJAX refreshing) without the user to refresh by him/herself or pressing F5 the whole time.

How would this work, precisely?

I'm creating a link-sharing website and on my index.php page (the page I want to refresh every 5 seconds) there are posts/links that must appear automatically (AJAX refreshing) without the user to refresh by him/herself or pressing F5 the whole time.

How would this work, precisely?

Share edited Dec 4, 2012 at 16:07 JMax 26.6k12 gold badges74 silver badges89 bronze badges asked Oct 12, 2011 at 13:39 JoeJoe 432 silver badges4 bronze badges 1
  • Take a look at this thread - a similar solution which uses jQuery. stackoverflow./questions/1406580/… – Steve Commented Oct 12, 2011 at 13:47
Add a ment  | 

4 Answers 4

Reset to default 4

You should use the setInterval javascript function to deal with this issue.

setInterval(callServer, REFRESH_PERIOD_MILLIS);

See:

  • some info on ajax Periodic Refresh
  • javascript setInterval documentation
  • [edit] some good refresh examples, especially without js framework (depending wether you want to use jquery, mototools, another or no framework...)

you have to user the setInterval method to call your ajax function to inject new content into your div:

  <HTML>
    <HEAD>
    <TITLE>Hello World Page</TITLE>
    <script language="JavaScript">

        function xmlhttpPost(strURL) {
            var xmlHttpReq = false;
            // Mozilla/Safari
            if (window.XMLHttpRequest) {
                xmlHttpReq = new XMLHttpRequest();
                if (xmlHttpReq.overrideMimeType) {
                    xmlHttpReq.overrideMimeType('text/xml');
                    // See note below about this line
                }
            // IE
            } else if (window.ActiveXObject) { // IE
                try {
                    xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
                }
            }
            if (!xmlHttpReq) {
                alert('ERROR AJAX:( Cannot create an XMLHTTP instance');
                return false;
            }   
            xmlHttpReq.open('GET', strURL, true);
            xmlHttpReq.setRequestHeader('Content-Type', 
                'application/x-www-form-urlencoded');        
            xmlHttpReq.onreadystatechange = function() { 
                callBackFunction(xmlHttpReq); 
            };
            xmlHttpReq.send("");
        }

        function callBackFunction(http_request) {
            if (http_request.readyState == 4) {
                if (http_request.status == 200) {
                    var responceString = http_request.responseText;
                    //TODO implement your function e.g.
                   document.getElementById("myDiv").InnerHTML+ = (responceString);
                } else {
                    alert('ERROR: AJAX request status = ' + http_request.status);
                }
            }
        }
      setInterval("xmlhttpPost('test.php')", 5000);
    </script>
    </HEAD>
    <BODY>
    Hello World

    <div id="myDiv"></div>
    </BODY>
    </HTML>

Is there a need to use AJAX? Unless I'm missing something; you could use the meta refresh tag:

<meta http-equiv="refresh" content="5">

I would remend increasing the time between refreshes as this will put a heavier load on the server and may cause to freeze, or slow down the site.

Use setInterval(myAjaxCallbackfunction,[time in ms]). Callback uses property of js that function are first class members(can be assigned to variables), and can be passed as argument to function for later use.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742266733a4411936.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信