2013-02-12 86 views
-1

你們可以告訴我怎樣才能讓jquery post每30秒對php腳本發出請求嗎? Php腳本正在向mysql寫入一些數據。如何讓jquery post每30秒向php腳本發送請求?

$.ajax(
{ 

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

    data: { 
    title: 'test', 

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

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

    //alert(bad) 

    } 


}); 
+1

您是否搜索過_at all_? – Daedalus 2013-02-12 05:13:37

+0

爲什麼不把ajax調用放在一個函數中,並使用javascript setInterval()函數每30秒調用一次你的ajax函數? – 2013-02-12 05:14:32

+0

[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 2013-02-12 05:16:40

回答

3

你可以使用設定的間隔不斷地輪詢像這樣。

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