Skip to content

BruceAn1978/WorkerThread.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WorkerThread.js

WorkerThread.js is a lightweight multi-thread lib that allow jsers to write code run between workers and DOM.

Docs & Examples

Quick Start

Example

  1. Start Http Server

    node httpd
    
  2. Add WorkerThread.js to your HTML File

    <script src="WorkerThread.js"></script>
    
  3. Write your code as:

    ///there are two ways to do task on work thread
    var t1 = new WorkerThread({i: 100} /*shared obj*/);
    
    ///1. use setInterval
    setInterval(function(){
    	t1.run(function(sharedObj){
    			return sharedObj.i++;
    		},
    		function(r){
    			console.log("t1>" + r.returnValue + ":" + r.error);
    		}
    	);
    }, 500);
    
    var t2 = new WorkerThread({i: 50});
    
    ///2. use sleep
    t2.run(function(sharedObj){	
    	while(this.threadSignal){
    		sharedObj.i++;
    
    		this.runOnUiThread(function(sharedObj){
    			///you can interact with dom here
    			W("body ul").appendChild("<li>"+sharedObj.i+"</li>");
    		});
    		
    		this.sleep(500);
    	}
    	return sharedObj.i;
    }, function(r){
    	window.console && console.log("t2>" + r.returnValue + ":" + r.error);
    });

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published