2

**I am having a tricky question that is I want to make a ajax call only 5 times per hour.I don't want to make 6th request within that hour.But after one hour exactly, I again make five ajax call with again one hour.I am using jquery some code I wrote like below.Any help? **

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

3 Answers3

1

Use setTimeOut();

$.ajax({
    url: "test.html",
    error: function(){
        // will fire when timeout is reached
    },
    success: function(){
        //do something
    },
    timeout: 12000// sets timeout to 12 seconds
});
Sardor Dushamov
  • 1,665
  • 3
  • 17
  • 44
0

What about like this idea?

Using Interval for every 1 minute, then 1 minute timeout to prevent dual request.

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)
  }
}
aldrien.h
  • 3,437
  • 2
  • 30
  • 52
0

Push all request arguments to a queue with a function called queueRequest and call checkQueue. checkQueue checks to see if there are items in the queue and if the number of active requests is less than 5. If these conditions are met, it pops a request from the queue and turns it into a real AJAX request. Then it attaches a done handler to the request that decreases the active request count and calls 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);
         });
     });
});
Zigri2612
  • 2,279
  • 21
  • 33