-1

Could you guys show me how i can make jquery post request to a php script every 30 seconds ? The Php script is writing some data to mysql.

$.ajax(
{

    type: 'POST',
    url: './postIt.php',

     data: {
     title: 'test',

     },
     success: function (good)
     {
     //handle success

     //alert(good)
     },
     failure: function (bad)
     {
     //handle any errors

     //alert(bad)

     }


});
user1788736
  • 2,727
  • 20
  • 66
  • 110
  • 1
    Did you search _at all_? – Daedalus Feb 12 '13 at 05:13
  • Why not put the ajax call in a function and use a javascript setInterval() function to call the your ajax function every 30 seconds? – Gregory-Turtle Feb 12 '13 at 05:14
  • possible duplicate of [How to send Ajax request on every 1s using JQuery?](http://stackoverflow.com/questions/5310118/how-to-send-ajax-request-on-every-1s-using-jquery) – Daedalus Feb 12 '13 at 05:16

2 Answers2

3

You could use set interval to continually poll like so.

    $(function() {
    setInterval(function() {
        $.ajax({
            type: 'POST',
            url: './postIt.php',

            data: {
                title: 'test',
            },
            success: function(good) {
                //handle success

                //alert(good)
            },
            failure: function(bad) {
                //handle any errors

                //alert(bad)

            }
        });
    }, 30000);
});
Paul Mendoza
  • 5,709
  • 12
  • 53
  • 82
1

You can use setTimeout or setInterval for this

indapublic
  • 2,208
  • 9
  • 38
  • 49