2

Is there any way to implement a multi-threading execution in Javascript. I am implementing a pagination in my application, with a target of the minimum waiting time for the user. Neither I want all the data to be brought to the client-side in one go, nor do I want to make a server as well as a DB hit on every "next/previous" button clicked. So I want the browser to create a request and update my data list, while the front end user has not reached the last page.

If there is anyone who has implemented this, then please suggest me.

Rustam
  • 1,875
  • 2
  • 16
  • 33
Sashi Kant
  • 13,277
  • 9
  • 44
  • 71
  • here the timing of the backend call will by dynamic, depending on the total (request-response time)/number of pages – Sashi Kant May 06 '12 at 07:02

4 Answers4

3

You can use setInterval of and setTimeOut function to gain multi-threaded effect but it is not true multi-threading you can read nice discussion here

Community
  • 1
  • 1
Adil
  • 146,340
  • 25
  • 209
  • 204
1

You can use timers (setTimeout) to simulate asynchronocity in JS. Timed events run when

  • The timer is reached
  • and when JS is not doing anything

So using setTimeout to create "gaps" in execution allow other "code in waiting" to be executed. It's still single threaded, but it's like "cutting the line"

Here's a sample

Joseph
  • 117,725
  • 30
  • 181
  • 234
1

You might consider looking at the infinite scrolling technique. There are a number of plugins out there that facilitate this, including Paul Irish's Infinite Scroll.

This is the same technique used by sites like Twitter so that the page updates as the user scrolls down, creating a seamless UI experience for the user and eliminating delays.

jamesmortensen
  • 33,636
  • 11
  • 99
  • 120
1

It is very easy to do it by using the Concurrent.Thread JavaScript library, which is free and open source towards this end. No SetInterval or SetTimeout required.

You can download it from here: http://sourceforge.net/apps/mediawiki/jsthread/index.php?title=Main_Page

Tutorial explaining the use of the library can be found here: http://www.infoq.com/articles/js_multithread