3

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>
8
  • 1
    You're missing the function argument to setInterval. Commented Aug 27, 2015 at 5:54
  • This should help: developer.mozilla.org/en-US/docs/Web/API/WindowTimers/… 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. Commented Aug 27, 2015 at 5:55
  • I tried renaming function () to function verify() and setInterval(verify, 10000); but that failed to work. 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. Commented Aug 27, 2015 at 5:57

4 Answers 4

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
//          ^^^^^^
Sign up to request clarification or add additional context in comments.

2 Comments

How did 10 seconds become 5 seconds?
@Barmar I saw comment saying 5 seconds so. I've updated to use 10 seconds
1

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);

Comments

1

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

http://www.w3schools.com/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 completition using some jQuery mechanisms like binding ajaxComplete or using when

1 Comment

You could also schedule the next call in the callback function of the previous one, using setTimeout instead of setInterval.
1

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 complete 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.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.