简体   繁体   English

如何每30秒向php脚本发出jquery发布请求?

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

Could you guys show me how i can make jquery post request to a php script every 30 seconds ? 你们能告诉我如何每30秒向php脚本发出一次jQuery post请求吗? The Php script is writing some data to mysql. 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)

     }


});

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

您可以为此使用setTimeoutsetInterval

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM