Friday, November 24, 2006

Java Script multi-threading

We all know that it is not possible,...but... using timers along with path-through routines that don't block might sound like a cool alternative. I have tried it and it works - picture this:

function doLongTask(x,y,z, callback)
{
this.X = x;
this.Y = y;
this.Z = z;
this.doneCallback=callback;
}

doLongTask.prototype.threadSpin = _longtask_threadspin;

function _longtask_threadspin()
{
//instead of long loops save your intermediate state in this.{vars}

this.X++;
if (this.X%100==0) this.Z = this.Y - 100; //some logic that would have been in a tight loop

if (this.X>1000) this.doneCallback();

}


And now we can easily:

var task = new doLongTask(1,2,3,doneLongTask);
var tmr = setInterval(task.threadSpin, 10/*time slice!!!*/);

function doneLongTask()
{
task.Z // result of task
clearInterval(tmr);// kill 'thread'
}

17 comments:

Anonymous said...

COOL, this should be ok - no closures problems either

itAdapter said...

Yes, speaking of closures I am trying to avoid this pattern:
this.X = function(){.....}
as much as I can because
eventually this will crash IE

Anonymous said...

helped, thanks

Anonymous said...

such a waste

Anonymous said...

I heard Chrome v8 compiles js to bin maybe those guys give access to real thing threads _xeh_

Anonymous said...

I'm newbie here, I hope to get friends at this forum

oakleyses said...

louboutin outlet, michael kors handbags clearance, air jordan, oakley vault, burberry outlet online, kate spade handbags, louis vuitton handbags, michael kors outlet online, ray ban sunglasses, cheap oakley sunglasses, michael kors outlet online, toms shoes, prada outlet, true religion outlet, jordan shoes, coach purses, longchamp handbags, michael kors outlet, polo ralph lauren outlet online, coach outlet store online, nike free, tiffany and co, polo ralph lauren outlet, prada handbags, oakley sunglasses, nike air max, longchamp handbags, true religion, nike free, kate spade outlet, nike shoes, tory burch outlet, nike air max, louis vuitton outlet online, coach outlet, coach factory outlet, true religion jeans, ray ban sunglasses outlet, air max, longchamp outlet, air max, gucci outlet, louboutin, cheap michael kors, chanel handbags

oakleyses said...

lululemon outlet, ray ban, nike roshe, mont blanc, converse shoes, karen millen, michael kors, roshe run, nfl jerseys, nike huarache, birkin bag, beats by dre, mac cosmetics, hogan, louboutin, converse pas cher, ray ban pas cher, montre pas cher, michael kors, vans, north face jackets, lululemon, oakley pas cher, north face outlet, reebok outlet, michael kors, nike free, ralph lauren, nike trainers, louis vuitton, louis vuitton, hollister, hermes, timberland, abercrombie and fitch, supra shoes, new balance shoes, juicy couture outlet, mulberry, air max, lancel, hollister, north face, rolex watches, juicy couture outlet, burberry, wedding dresses, new balance pas cher, abercrombie and fitch, nike air max

oakleyses said...

nike air max, air max pas cher, p90x3, converse, ugg boots, ferragamo shoes, giuseppe zanotti, louboutin, moncler, mcm handbags, air max pas cher, ralph lauren, ugg outlet, soccer jerseys, valentino shoes, herve leger, babyliss pro, moncler, moncler, jimmy choo, vans, asics gel, air max, moncler outlet, chi flat iron, canada goose outlet, uggs on sale, hollister, canada goose, instyler ionic styler, moncler, canada goose, insanity, marc jacobs, tn pas cher, ray ban, canada goose outlet, air max, soccer shoes, canada goose, bottega veneta, ghd, ugg, celine handbags, ugg, gucci, canada goose, ugg boots, oakley

oakleyses said...

kate spade outlet online, nike air max, tory burch outlet online, ugg boots clearance, prada handbags, nike air max, ray ban sunglasses, michael kors handbags, chanel handbags, jordan shoes, nike outlet, longchamp outlet online, gucci handbags, christian louboutin, christian louboutin shoes, burberry outlet online, ugg outlet, oakley sunglasses, louis vuitton outlet, ray ban outlet, nike free, cheap oakley sunglasses, cheap ugg boots outlet, louis vuitton, longchamp outlet, michael kors outlet online, polo ralph lauren outlet, burberry outlet online, christian louboutin outlet, michael kors outlet online, oakley sunglasses, ray ban sunglasses, oakley vault, ugg australia, cheap ugg boots, red bottom shoes, replica watches, michael kors outlet, tiffany and co, tiffany jewelry, louis vuitton outlet online, replica watches, polo ralph lauren, prada outlet, louis vuitton handbags

mmjiaxin said...

ferragamo shoes
michael kors outlet
rolex watches outlet
tory burch outlet online
michael kors handbags
coach outlet store
mac cosmetics
michael kors outlet online
wellensteyn outlet
michael kors outlet
canada goose outelt
air max 2014
ray ban,rayban,occhiali ray ban,ray-ban,ray ban occhiali,ray ban sunglasses
woolrich outlet store
fred perry polo shirts
michael kors outlet
tiffany outlet
canada goose jackets
oakley outlet store
mizuno running shoes
mm1202

Unknown said...

ravens jerseys
michael kors handbags
hermes belt
fitflops shoes
abercrombie and fitch
michael kors outlet store
michael kors handbags
nike air huarache
bears jerseys
michael kors handbags

KIMI HIME said...

Thank you for this very useful health article

Cara Mengobati Radang Amandel
Cara Mengatasi Perut Kembung
Cara Mengobati Radang Sendi
Obat Penghilang Benjolan Bernanah

Pengobatan Scabies secara Alami said...


It is great to have visited your website. Thanks for sharing useful information. And also visit my website about health. God willing it will be useful too

Obat Penghilang Jerawat dan Bekasnya
Cara Menyembuhkan Batuk Berkepanjangan
Cara Mengobati Pengapuran Tulang
Obat Pengapuran Otak pada Anak
Cara Mengatasi Mata Bengkak
Obat Herpes Zoster

Unknown said...

2018531 leilei3915
canada goose
ray ban sunglasses
pandora outlet
coach outlet store online
pandora charms
kate spade outlet online
michael kors handbags
christian louboutin shoes
louis vuitton outlet online
true religion

yanmaneee said...

golden goose outlet
goyard bags
golden goose outlet
golden goose sneakers
yeezy shoes
adidas yeezy
goyard wallet
yeezy supply
supreme clothing
adidas yeezy

Susan said...

This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information. Keep it up. Keep blogging. Looking to reading your next post. Wondering where to go in 2020? Things to do has ranked as the best include a remote, idyllic island and the design capital.