最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I load a PHP file every 5 seconds with JavaScript - Stack Overflow

programmeradmin4浏览0评论

I want a php program to be executed every 5 seconds using JavaScript. How can I do that?

I tried using:

<script type="text/javascript">
    setInterval(
        function (){
            $.load('update.php');
        },
        5000
    );
</script>

But it doesn't work.

I want a php program to be executed every 5 seconds using JavaScript. How can I do that?

I tried using:

<script type="text/javascript">
    setInterval(
        function (){
            $.load('update.php');
        },
        5000
    );
</script>

But it doesn't work.

Share Improve this question edited Dec 28, 2012 at 9:00 Pranav 웃 8,4776 gold badges40 silver badges48 bronze badges asked Dec 28, 2012 at 8:53 user1763032user1763032 4271 gold badge9 silver badges24 bronze badges 5
  • 2 you could use a non-blocking server such as nodejs, or implement your php websocket server – Jonathan de M. Commented Dec 28, 2012 at 8:53
  • AJAX request on a 5 second timer. (Not saying it's a good idea) – Cerbrus Commented Dec 28, 2012 at 8:55
  • Use setTitmeout() and ajax – Vladimir Gordienko Commented Dec 28, 2012 at 8:55
  • better set up a crone job – mike_hornbeck Commented Dec 28, 2012 at 8:57
  • you should make sure that jQuery is loaded at this time so start with $(function() { ... your set interval and other code... }); then it should work. – wildhaber Commented Dec 28, 2012 at 22:01
Add a comment  | 

2 Answers 2

Reset to default 17

Using jQuery and setInterval:

setInterval(function() {
    $.get('your/file.php', function(data) {
      //do something with the data
      alert('Load was performed.');
    });
}, 5000);

Or without jQuery:

setInterval(function() {
    var request = new XMLHttpRequest();
    request.onreadystatechange = function() {
         if (request.readyState == 4 && request.status == 200) {
            console.log(request.responseText);
         }
      }
    request.open('GET', 'http://www.blahblah.com/yourfile.php', true);
    request.send();

}, 5000);

Try using setInterval() to perform a XHR call. (jQuery, non jQuery)

setInterval(function() {
    // Ajax call...
}, 5000);

This will execute your code inside the function every 5 secs

发布评论

评论列表(0)

  1. 暂无评论