Ajax 请求,每 30 秒自动接收一次数据(多个用户同时使用网站)



我在下面有ajax请求 - 它将获得用户的一些承诺(瓷砖(,其中当有承诺时,瓷砖将翻转并显示:

var reveal = 0;
$.ajax({
type: 'GET',
dataType: "json",
url: '/Services/getpledge.ashx',
success: function (data) {
  if (data.status === "Success") {
    $.each(data.Pledges, function (i, v) {
      if (typeof v.TileID !== 'undefined') {
        jQuery('.square-container div[data-id=' + v.TileID + ']').find('.front div').removeClass('box1').addClass('box3');
        jQuery('.square-container div[data-id=' + v.TileID + ']').parent().addClass('complete');
        jQuery('.square-container div[data-id=' + v.TileID + ']').attr('data-content', v.PledgeContent);
        jQuery('.square-container div[data-id=' + v.TileID + ']').attr('data-smoke', v.isSmoker);
        jQuery('.square-container div[data-id=' + v.TileID + ']').attr('data-fb_uid', v.FBID);
        if (v.Lastname != null)
        jQuery('.square-container div[data-id=' + v.TileID + ']').attr('data-name', v.Firstname + ' ' + v.Lastname);
        else
        jQuery('.square-container div[data-id=' + v.TileID + ']').attr('data-name', v.Firstname);
      }
      reveal += 1;
    });
    //reveal = data.Pledges.length;
    //console.log(data.Pledges.length + ' ' + reveal);
    $('.changeMe').text(reveal + '/' + number_square);
  }
},
error: function () {
  console.log('An error has occured, please refresh page and try again');
}
});

在发布当天,将有许多用户同时使用该网站,所以我想知道如何确保请求自动完成 - 每 30 秒一次,以便页面始终更新。

您可以简单地执行以下操作,每 30 秒运行一次。

window.setInterval(function() {
    // code you want to run..
}, 30000);

最新更新