calculate request timeout in js | Community
Skip to main content
Adobe Employee
June 2, 2021
Solved

calculate request timeout in js

  • June 2, 2021
  • 1 reply
  • 983 views

Hi, 

 

I have got the requirement to set maximum timeout in my ajax call.

 

10Mbps = 1mB/s upload ... 1GB file = 14min upload * 2 = 28 min 
10Mbps = 1mB/s upload ... 500MB file = 7min upload * 2 = 14 min 
10Mbps = 1mB/s upload ... 5MB file = 4 sec upload * 2 = 8 sec 

 

How can I get the timeout using the network speed and the file size?

 

Thank you

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by BrianKasingli

@keerthana_h_n 

The XMLHttpRequest.timeout property is an unsigned long representing the number of milliseconds a request can take before automatically being terminated. The default value is 0, which means there is NO_TIMEOUT

 

Also, while your computer request for a download, and the server responds with downloadable content, then you wouldn't get a timeout from your AJAX request. Remember, the purpose of a timeout is to prevent a device from endlessly waiting for a sever to respond.

 

However, if you would like to add a timeout, your request would look something like this:

 

 

var xhr = new XMLHttpRequest(); xhr.open('GET', '/server', true); xhr.timeout = 3000; // time in milliseconds, 3 seconds xhr.onload = function () { // Request finished. Do processing here. }; xhr.ontimeout = function (e) { // XMLHttpRequest timed out. Do something here. }; xhr.send(null);

 

 

JQUERY would look something like this:

 

 

$.ajax({ url: "/server", error: function(){ // will fire when timeout is reached }, success: function(){ //do something }, timeout: 3000 // time in milliseconds, 3 seconds });

 

 

 

1 reply

BrianKasingli
Community Advisor and Adobe Champion
BrianKasingliCommunity Advisor and Adobe ChampionAccepted solution
Community Advisor and Adobe Champion
June 2, 2021

@keerthana_h_n 

The XMLHttpRequest.timeout property is an unsigned long representing the number of milliseconds a request can take before automatically being terminated. The default value is 0, which means there is NO_TIMEOUT

 

Also, while your computer request for a download, and the server responds with downloadable content, then you wouldn't get a timeout from your AJAX request. Remember, the purpose of a timeout is to prevent a device from endlessly waiting for a sever to respond.

 

However, if you would like to add a timeout, your request would look something like this:

 

 

var xhr = new XMLHttpRequest(); xhr.open('GET', '/server', true); xhr.timeout = 3000; // time in milliseconds, 3 seconds xhr.onload = function () { // Request finished. Do processing here. }; xhr.ontimeout = function (e) { // XMLHttpRequest timed out. Do something here. }; xhr.send(null);

 

 

JQUERY would look something like this:

 

 

$.ajax({ url: "/server", error: function(){ // will fire when timeout is reached }, success: function(){ //do something }, timeout: 3000 // time in milliseconds, 3 seconds });