ThreadJS Example 1

Simple job with array iteration

by ashanfer

HTML

<script src="https://cdn.rawgit.com/AshanFernando/ThreadJS/master/thread.min.js"></script>
<h2>Example 1:</h2>
<p> Following code uses a new Thread to compute the summation of numbers from 1 - 1000,000,000 and returns the results to main Thread.</p>
<strong>Run the fiddle to compute the results ...</strong>
<p id = 'result'></p>

JavaScript

var thread = new Thread();
thread.start(1000000000, function (size) {
    var x = 0;
    for (var i = 1; i <= size; i++) {
        x = x + i;
    }
    return x;
}).then(function (result) {
    $('#result').text('Result: ' + result);
    this.close();
}).fail(function (error) {
    $('#result').text('Error: ' + error);
    this.close();
});