JavaScript Solutions, Competitive programming in JavaScript, MCQ in JS

Thursday, 29 August 2019

Javascript detect connection speed

JavaScript detect connection speed

Ever feel like your internet is running slow? Webpages seem sluggish while uploads and downloads take forever! It's very frustrating, especially when you pay for high-speed internet.

As a developer, we need to find the current speed and load the pages accordingly. So if you want to get the connection speed in JS you can use the Navigator.connection object.

Let's take an example:

navigator.connection.addEventListener('change', () => {
let currRtt = navigator.connection.rtt;
let effType = navigator.connection.effectiveType;
console.log('current connections', currRtt, effType)

so the above example will trigger an event whenever there will be some changes in the network.

So let's test it by changing the network connections.
Javascript detect connection speed

As you can see in logs whenever I changed the network type from network tab events gets fired and it printed the current connections types.

Javascript detect connection speed example

I hope you will get more idea about the connection object from Javascript.

No comments:

Post a Comment