Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

calculate request timeout in js

Avatar

Avatar
Boost 5
Level 2
keerthana_hn
Level 2

Likes

5 likes

Total Posts

18 posts

Correct Reply

0 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
Validate 1
View profile

Avatar
Boost 5
Level 2
keerthana_hn
Level 2

Likes

5 likes

Total Posts

18 posts

Correct Reply

0 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
Validate 1
View profile
keerthana_hn
Level 2

02-06-2021

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

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

611 likes

Total Posts

588 posts

Correct Reply

230 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

611 likes

Total Posts

588 posts

Correct Reply

230 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
BrianKasingli
MVP

02-06-2021

@keerthana_hn 

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
});

 

 

 

Answers (0)