Simulate Parallel Execution In jQuery - zParallel

Simulate Parallel Execution In jQuery - zParallel
File Size: 4.67 KB
Views Total: 77
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   

zParallel is a jQuery plugin that simulates parallel execution of functions during idle time. Can be used as a great alternative to the jQuery .each() function.

How to use it:

1. Load the main script zParallel.js after jQuery.

<script src="/path/to/cdn/jquery.min.js"></script>
<script src="/path/to/cdn/zParallel.js"></script>

2. Initialize the plugin and specify the function to run on which select as follows:

$(document).zParallel({
  name: "example",
  selectorToRun: "SELECTOR",
  funcRun: functionToRun
});

3. A real world example:

var tab1 = $('#tbl1');
for (i = 0; i < 1000; i++)
    $("<tr>"+
    "<td>#" + i + "</td>"+
    "<td><input id='a_" + i + "' value='" + i + "' >"+
    "</td><td><input id='b_" + i + "' value='" + i + "' ></td></tr>")
        .appendTo(tab1);

$(document).zParallel({
    name: "A",
    selectorToRun: "input[id^='a_']",
    funcRun: function (nextThis)
    {
        var $this = $(nextThis);

        var nowDateTime = Date.now();
        var i = 0;
        while( Date.now() < nowDateTime + 2)
              i++;

        $this.val( i );
        if (i > 100)
            $this.css('color', 'green').css('font-weight', 'bold');
        else
            $this.css('color', 'blue');
    }
});


$(document).zParallel({
    name: "B",
    selectorToRun: "input[id^='b_']",
    funcRun: function (nextThis)
    {
        var $this = $(nextThis);

        var nowDateTime = Date.now();
        var i = 0;
        while( Date.now() < nowDateTime + 2)
              i++;

        $this.val( i );

        if (i > 100)
            $this.css('background', '#BBFFBB');
        else
            $this.css('background', '#FFBBBB');
    }
});

4. Full plugin configs.

$(document).zParallel({
  selectorToRun: null,
  funcRun: null,
  lengthToRun: 0,
  iterScheduled: 0,
  ticksToRun: 50,
  showDebugInfo: true
});

This awesome jQuery plugin is developed by uspnick. For more Advanced Usages, please check the demo page or visit the official website.