prudvi raju
prudvi raju

Reputation: 505

Achieve multithreading in javascript

var url = 'reports?type=travel&sttime=22/01/2015&endtime=2/03/2015'
$.get(url).done(function(response){console.log(response);});

this is my code to fetch the reports data in between 22/01/2015 and 2/03/2015.

Because the time span is 40 Days and response will contain huge data.. and it is taking more than 1 min to get the data .

now my Question is , i am diving the dates in 40/10 = 4 i.e 22 Jan-1 Feb, 2 Feb-11 Feb,12 Feb-21 Feb,21 Feb- 02 Mar

I am creating four URL's as per the date partitions.. Is there any way of doing multi-threading to achieve all the urls hit the data at a time and fetch the response..to reduce the display time in my browser.

Upvotes: 0

Views: 83

Answers (4)

Guffa
Guffa

Reputation: 700152

You don't need multithreading to do multiple requests. The requests are asynchronous, so you can just make the calls after each other, and they will start at the same time:

$.get(url1).done(function(response){console.log(1, response);});
$.get(url2).done(function(response){console.log(2, response);});
$.get(url3).done(function(response){console.log(3, response);});
$.get(url4).done(function(response){console.log(4, response);});

The done callback for each request will be called when the response arrives.

Whether this will actually speed up the loading depends on where the bottle neck is.

You can use the when method to wait for all the requests to finish:

$.when(
  $.get(url1),
  $.get(url2),
  $.get(url3),
  $.get(url4)
).done(function(d1, d2, d3, d4){
  var result = d1.concat(d2, d3, d4);
  // ...
});

Upvotes: 0

Innovation
Innovation

Reputation: 1524

I suggest you to use HTML5 Web Workers.

http://www.w3schools.com/html/html5_webworkers.asp

This is the only way to work for these type of applications but unfortunately not all browsers support them. So you should have to keep an eye on browsers and version that you want to suggest.

Also for improving the UI .you can use offline storage at client side.

http://www.w3schools.com/html/html5_webstorage.asp

Example: I have a data between 1st feb to 20th feb.So when first time user load the website i fetch all the data and store it at client side.Now again when user open website on 22nd feb ,this time i will only fetch data between 20th feb and 22nd feb and again store it at client side.So only at first time this will take time and from second time onwards it improve user interaction and is more efficient.Most of the mordern html5 based websites which show insights will work in the similar manner.

Upvotes: 0

Jillberth Estillore
Jillberth Estillore

Reputation: 55

No there isn't. JavaScript doesn't support multi-threading because the JavaScript interpreter in the browser itself is single-threaded.

Upvotes: 0

Madara's Ghost
Madara's Ghost

Reputation: 174937

It wouldn't matter. The bottleneck here is likely not the I/O, but the network speed. It won't matter if you open 2 or 4 or 10 concurrent connections if your client's network can only transfer N kB/s from your server to his machine.

A better solution would be to paginate the data, only fetch the portions the client currently needs. I doubt any user can read such huge amounts of data that pertains to two whole months. Fetch only the part the client is likely to be interested in, maybe download pages 2 and 3 as well, and then download the rest in the background.

Upvotes: 1

Related Questions