-3

I'm a newby in web development. Sorry about basic questions.

I have a code block with ajax. It takes data from api, and it has to apply every 10 seconds db updates to page. But it's didn't worked. Why my setTimeout() or setInterval() code getting too much response? I tried both of them.

$(document).ready(function() {
   sendRequest();
    function sendRequest() {
        $.ajax({
            url: "http://127.0.0.1:8000/studentsapi",
            dataType: "json",
            success: function(data) {
                $('#first_name').text(data[0].first_name);
                $('#last_name').text(data[0].last_name);
                $('#age').text(data[0].age);
                $('#gender').text(data[0].gender);
            },
            complete: function(data) {

                setTimeout(sendRequest, 10000); // 
            }
        });
    }
});

In terminal:

enter image description here

1 Answers1

0

As I understand the response time of your API is fast and you just need to clear the timeout. For example:

const timeout = null;
// Your request code
complete: {
  if (!timeout) {
    timeout = setTimeout(sendRequest, 10000)
    return
  }
  clearTimeout(timeout)
}

Also try to make a research on debounce and throttling it can help you achieve that easier, because you use JQuery as I see. Also you can just use setInterval outside the complete property.

setInterval(request, 10000)

No need to do it inside complete property unless it is a requirement

Alopwer
  • 611
  • 8
  • 22
  • 3
    It's not clear to me that the code as posted could cause the posted result. It doesn't matter if the response is "fast", because the timeout is 10 seconds long, and it doesn't get restarted until the ajax call completes. – Pointy Aug 18 '22 at 13:16
  • @Pointy I disagree. I am willing to bet the user keeps clicking something that calls `sendRequest` and it keeps adding new timeouts. If OP answers my question this answer will be one way to handle it. – epascarello Aug 18 '22 at 13:18
  • @Pointy, the question itself doesn't have clear requirements, so I've tried to explain how I understood it – Alopwer Aug 18 '22 at 13:19
  • @epascarello oh sure, *something* is causing that process to get started, and it could be some event, a `setInterval()`, mentioned in the question but not present, or anything else. – Pointy Aug 18 '22 at 13:19
  • @Pointy, I already explained why it happens, she just calls setTimeout every time the request completes, imagine all setTimeouts stacked for 10 seconds, and then they just BOOM, and make calls every second – Alopwer Aug 18 '22 at 13:21
  • But per-request, there's only **one** call to `setTimeout()`. No other call will be made until that request completes. So, ajax HTTP goes out, returns after x milliseconds, and a new timer is started. That process will be repeated 10 seconds later. – Pointy Aug 18 '22 at 13:24
  • Now, as I mentioned, clearly *something* is wrong, but it's not in the posted code as far as I can tell. – Pointy Aug 18 '22 at 13:25