bagofmilk
bagofmilk

Reputation: 1550

At startup, run AJAX request every 10 seconds

This is in response to the SO problem found here -> Polling every 10 seonds

That SO post helped, but when I tried to conform it to my needs it just wasn't working out.

I have a php script that will input random data into my database. The script works fine when i point my browser directly to 'create_new_job.php'. My problem is that I cannot seem to run an ajax request to run the script every 10 seconds once my main page loads.

My php script to insert data into database: 'create_new_job.php'

My main page: index.php

This is in my index.php:

<script>
    function newjobs() {
    var inputjob = $.ajax({
                    url: "create_new_job.php",
                    complete: schedulenewjobs
                    });
    inputjob.done(function(data) { alert('Job added!'); }
    inputjob.fail(function(data) { alert('Job not added....'); }
}

function schedulenewjobs() {
    setTimeout(function() { newjobs(); }, 10000);
}
</script>

First, I'm getting an error at line

inputjob.fail(.....

But when I remove the 'done' and 'fail' notices, nothing happens. No data is inserted and I'm not receiving any errors.

I'm not trying to retrieve any data - I'm only wanting to run 'create_new_job.php' every 10 seconds.

Any help would be greatly appreciated.

Upvotes: 2

Views: 3500

Answers (4)

Faruque Ahamed Mollick
Faruque Ahamed Mollick

Reputation: 792

I think this post will help you: Send AJAX request every n seconds using jQuery

sendRequest();

 function sendRequest(){
  $.ajax({
    url: "example.php",
    success: 
      function(data){
       $('#listposts').html(data); 
    },
    complete: function() {
   setInterval(sendRequest, 10000); // Call AJAX every 10 seconds
  }
 });
};

Upvotes: 4

Richard Christensen
Richard Christensen

Reputation: 2166

$(document).ready(function(){
  setInterval(
    newjobs(),
    10000
  );
});

function newjobs() {
  inputjob = $.ajax({
    url: "create_new_job.php",
  })
    .done(function(data) { alert('Job added!'); })
    .fail( function(data) { alert('Job not added....'); });
}

Upvotes: 1

adeneo
adeneo

Reputation: 318202

You're not properly closing your functions, and to run the function every ten seconds, you could make it recursive, calling it again from the done() callback, or you could use an interval

(function newjobs() {
    var inputjob = $.ajax({
                        url: "create_new_job.php"
                   });
    inputjob.done(function(data) {  
        alert('Job added!'); 
        setTimeout(newjobs, 10000); // recursion
    });
//   ^^ missing closing

    inputjob.fail(function() { 
        alert('Job not added....'); 
    });
})();

Upvotes: 5

Akhil Sidharth
Akhil Sidharth

Reputation: 746

try setInterval function

setInterval(function() {
    function newjobs() {
    var inputjob = $.ajax({
                    url: "create_new_job.php",
                    complete: schedulenewjobs
                    });
    inputjob.done(function(data) { alert('Job added!'); }
    inputjob.fail(function(data) { alert('Job not added....'); }
}
}, 10 * 1000);

Upvotes: -1

Related Questions