如何每隔30秒对php脚本发出jquery post请求?

时间:2013-02-12 05:12:04

标签: javascript jquery

你们能告诉我如何每隔30秒向一个php脚本发出jquery post请求吗? Php脚本正在向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)

     }


});

2 个答案:

答案 0 :(得分:3)

你可以使用set interval来连续轮询。

    $(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);
});

答案 1 :(得分:1)

您可以将setTimeoutsetInterval用于此