Skip to content

API Document

deep-rain edited this page Mar 7, 2017 · 1 revision

Thread.js API Document

Usage

var thread = new Thread(function(){
    return "hello thread.js";
});

thread.once().done(function(res){
    console.log(res);    // -> hello thread.js
});

API

constructor

new Thread(func, [depends])

arguments description
func run by calling execute () or once ().
[depends] Dependent libraries as Array or String

example

new Thread(function(a, b){
    return a + b;
})

once

thread.once(args1, args2, ...)

Run the function passed by the constructor.

This is done only once, after which the object is terminated.

arguments description
args Arguments passed to the function.

example

thread.once(1, 2);

execute

thread.execute(args1, args2, ...)

Run the function passed by the constructor.

Usually, please use once().

arguments description
args Arguments passed to the function.

example

thread.execute(1, 2);

terminate

thread.terminate()

Instantly terminate the Thread object. Even if a thread is processing it, it forcibly unloads from memory.

arguments description
(no arguments) (no description)

example

thread.terminate();

done, fail, always

thread.done(func) thread.fail(func) thread.always(func)

A method that defines the behavior of processing success (done), failure (fail), and always.

The function is stored in the queue, and when one of the above events is issued, it is called in the order in which it was set.

arguments description
func Function called upon completion of processing.

example

thread.done(function(res){
    console.log(res);
});

progress

thread.progress(func)

Called by the notify method in the local scope of the function passed to the constructor.

arguments description
func Function called upon notify() method.

example

    var thread = new Thread(function(){
        for(var i = 0; i <= 100; i++){
            for(var j = 0; j < 10000000; j++){
                // .. Very slow processing
            }
            notify(i); 
        }
    });
    
    thread.once().progress(function(i){
        console.log(i);
    }).done(function(){
        console.log("done!");
    });

Clone this wiki locally