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

问题描述 投票:0回答:1

我在下面有一个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秒一次,以便页面始终更新。

javascript jquery ajax
1个回答
0
投票

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

window.setInterval(function() {
    // code you want to run..
}, 30000);
© www.soinside.com 2019 - 2024. All rights reserved.