【问题标题】:How can I call an Ajax Request in a specific time Period?如何在特定时间段内调用 Ajax 请求?
【发布时间】:2016-03-11 05:18:40
【问题描述】:

**我有一个棘手的问题,我想每小时只进行 5 次 ajax 调用。我不想在那一小时内发出第 6 次请求。但是一小时后,我再次发出 5 个 ajax再打电话一小时。我正在使用 jquery 一些我写的代码,如下所示。有帮助吗? **

function getBubbles(){
    $.ajax({
        type : 'POST',
        url  : '/PostData',
        data : data,
        success : function(response){
            console.log(response);
        }
    });
}

【问题讨论】:

  • 用户停留在页面上的平均时间是多少?
  • 为什么用户会保持一个页面打开而不重新加载或更改到其他页面一个小时? :(
  • 这里我用的是facebook的好友请求。在一小时内我只能向五个成员发送好友请求。我不能发出不超过五个的请求

标签: jquery


【解决方案1】:

使用 setTimeOut();

$.ajax({
    url: "test.html",
    error: function(){
        // will fire when timeout is reached
    },
    success: function(){
        //do something
    },
    timeout: 12000// sets timeout to 12 seconds
});

【讨论】:

  • 或者你可以使用 setinterval ...两者都是一样的,所以作为评论而不是回答
  • 如何每小时调用5次函数?
【解决方案2】:

这样的想法怎么样?

每 1 分钟使用间隔,然后 1 分钟超时以防止双重请求。

5 Times request every HOUR

var startRequest = setInterval(getBubbles, 60000); // change this part for time of request interval
var valid_minutes = [12, 24, 36, 48, 60]; // I divided 60minutes into 5 possible/valid time.

function getBubbles() {
  var getMinutes = new Date().getMinutes();
  var is_valid = $.inArray(getMinutes, valid_minutes); //following will return -1 (if not found) because a number is being searched in an array

  if (is_valid >= 0) {
    setTimeout(fucntion(
      // do your ajax functions here to execute
      $.ajax({
        type: 'POST',
        url: '/PostData',
        data: data,
        success: function(response) {
          console.log(response);
        }
      });
    ), 60000); //set timeout to 60 seconds to prevent multiple request (will used to past 1 minute interval)
  }
}

【讨论】:

    【解决方案3】:

    使用名为 queueRequest 和调用 checkQueue 的函数将所有请求参数推送到队列。 checkQueue 检查队列中是否有项目以及活动请求的数量是否小于 5。如果满足这些条件,它会从队列中弹出一个请求并将其转换为真正的 AJAX 请求。然后它为请求附加一个 done 处理程序,以减少活动请求计数并调用 checkQueue。

    var count = 0;          // Number of functions being called
    var funcArray = [];     // Array of functions waiting
    var MAX_REQUESTS = 5;   // Max requests
    var CALL_WAIT = 100;        // 100ms
    
    function call()
    {
        // Check if count doesn't exceeds or if there aren't any functions to call
        if(count >= MAX_REQUESTS || funcArray.length == 0)
            // Call call() after 100ms
            setTimeout(function() { call() }, CALL_WAIT);
        count++;            // Add request to the counter
        var func = funcArray.pop();
        $.ajax(..., function(data)
        {
            func(data);
            // .......
            count--;        // Substract request to the counter
        });
    }
    
    $(function()
    {
        call();     // First call to start polling. It will call itself each 100ms
    });
    
    $(function()
    {
        $("div.server").each(function() {
             funcArray.push(function(data)
             {
                alert(data);
             });
         });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多