如何让jquery每30秒向php脚本发布一次请求

How to make jquery post request to php script every 30 seconds?

本文关键字:脚本 php 布一次 请求 30秒 jquery      更新时间:2023-09-26

你们能告诉我如何每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)
     }

});

您可以使用设置的间隔来连续轮询。

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

您可以对此使用setTimeout或setInterval