admin管理员组

文章数量:1316329

I'm trying to use setInterval to execute the php script update.php every 10 seconds and refresh div id = verification. For some reason setInterval is preventing the script from functioning. Any suggestions on where to place\change setInterval would be appreciate as I'm stumped (sorry entry level javascript user here). For clarity I omitted all the non-relevant details, such as vars.

<div id="verification"></div>

<script id="verification" language="javascript" type="text/javascript">
$(function() {
    $.ajax({
        url: 'update.php', //php          
        data: "", //the data "caller=name1&&callee=name2"
        dataType: 'json', //data format   
        success: function(data) //on receive of reply
            {
                var foobar = data[2]; //foobar
                $('#verification').html("(<b>" + foobar + "</b>)"); //output to html

            }
    });
});

setInterval(10000); //every 5 secs
</script>

I'm trying to use setInterval to execute the php script update.php every 10 seconds and refresh div id = verification. For some reason setInterval is preventing the script from functioning. Any suggestions on where to place\change setInterval would be appreciate as I'm stumped (sorry entry level javascript user here). For clarity I omitted all the non-relevant details, such as vars.

<div id="verification"></div>

<script id="verification" language="javascript" type="text/javascript">
$(function() {
    $.ajax({
        url: 'update.php', //php          
        data: "", //the data "caller=name1&&callee=name2"
        dataType: 'json', //data format   
        success: function(data) //on receive of reply
            {
                var foobar = data[2]; //foobar
                $('#verification').html("(<b>" + foobar + "</b>)"); //output to html

            }
    });
});

setInterval(10000); //every 5 secs
</script>
Share Improve this question edited Aug 27, 2015 at 6:17 Tushar 87.2k21 gold badges163 silver badges181 bronze badges asked Aug 27, 2015 at 5:53 michellemichelle 6532 gold badges6 silver badges23 bronze badges 8
  • 1 You're missing the function argument to setInterval. – Barmar Commented Aug 27, 2015 at 5:54
  • This should help: developer.mozilla/en-US/docs/Web/API/WindowTimers/… – Drakes Commented Aug 27, 2015 at 5:55
  • Didn't you post a very similar question a few hours ago? The difference there was that it used setTimeout instead of setInterval. – Barmar Commented Aug 27, 2015 at 5:55
  • I tried renaming function () to function verify() and setInterval(verify, 10000); but that failed to work. – michelle Commented Aug 27, 2015 at 5:56
  • It was flagged as a duplicate question, which is incorrect. I need help with basic syntax not just setInterval. – michelle Commented Aug 27, 2015 at 5:57
 |  Show 3 more ments

4 Answers 4

Reset to default 2

Suggestions/Steps/Bugs:

  1. Create a separate function to perform ajax request
  2. Call this function on page load to run it when page is loaded
  3. Use setInterval() to call the function every n seconds
  4. id should always be unique. You're now using verification as if for <div> and <script>
  5. You can remove id and language attributes of <script>. Those are not required.

Code:

function update() {
    $.ajax({
        url: 'update.php', //php          
        data: "", //the data "caller=name1&&callee=name2"
        dataType: 'json', //data format   
        success: function (data) {
            //on receive of reply
            var foobar = data[2]; //foobar
            $('#verification').html("(<b>" + foobar + "</b>)"); //output to html
        }
    });
}

$(document).ready(update); // Call on page load
//                ^^^^^^


setInterval(update, 10000); //every 10 secs
//          ^^^^^^

setInterval() takes a function (that it should execute) as it's first argument.

Try this:

setInterval(function(){
  $.ajax({                                      
      url: 'update.php', //php          
      data: "",                        //the data "caller=name1&&callee=name2"
      dataType: 'json',                //data format   
      success: function(data)          //on receive of reply
      {
          var foobar = data[2];           //foobar
          $('#verification').html("(<b>"+foobar+"</b>)");     //output to html
      } 
  });
}, 10000);

you are using setInterval in a wrong way - you can read about it here:

http://www.w3schools./js/js_timing.asp

Also please notice that AJAX calls are asynchronous - when program is going forward it doesn't mean that previous AJAX call has ended. You can "wait" for AJAX pletition using some jQuery mechanisms like binding ajaxComplete or using when

You are missing the function block:

setInterval(function() {
  // to do
}, 5000);

My suggestion is to go for setTimeout as you are running ajax it is not certain that it would plete within 5 seconds. In case if you wana go for it:

var dispatchUpdates = function() {
  setTimeout(pullUpdates, 5000);
};

var pullUpdates = function() {
  $.ajax({
    url: 'update.php',
    data: "",
    dataType: 'json',
    success: function(data) {
      var foobar = data[2];
      $('#verification').html("(<b>" + foobar + "</b>)");
      dispatchUpdates(); // next updates
    },
    error: function() {
      dispatchUpdates(); // retry
    }
  });
};

$(dispatchUpdates); //page load
<script src="https://ajax.googleapis./ajax/libs/jquery/1.9.1/jquery.min.js"></script>

本文标签: javascriptrun php script every 10 secondsStack Overflow