JS multi threading thread javascript example

For all browsers e.g. old IE 11 ….

 
 <script>
 
 
 
        var mbThreadOneRunning = false;
        var mbThreadOneEnded = false;
 
        var mbThreadTwoRunning = false;
        var mbThreadTwoEnded = false;
 
        // you can create more than one Thread 
        // setTimeout() used for running more Threads at once
        var timeout_1 = setTimeout("ThreadOne()", 5000); // 5000 == 5 second
        var timeout_2 = setTimeout("ThreadTwo()", 3000); // 
 
        function ThreadOne() {
            mbThreadOneRunning = true;
            alert("1. ThreadOne working!");
            // working code here .............
            mbThreadOneEnded = true;
            clearTimeout(timeout_1);
        }
 
        function ThreadTwo() {
            mbThreadTwoRunning = true;
            alert("2. ThreadTwo working!");
            // working code here .............
             mbThreadTwoEnded = true;
            clearTimeout(timeout_2);
        }
 
 
        var mInterval = setInterval("checkThread()", 1000); // 1000 ms == 1 second, you can put smaller value
 
        function checkThread() {
            if (mbThreadOneEnded == true) {                
                alert("1. checkThread() ThreadOne finish work! " + mbThreadOneEnded);
            }
            else if (mbThreadTwoEnded == true) {
                alert("2. checkThread() ThreadTwo  finish work! " + mbThreadTwoEnded);
            }
            if (mbThreadOneEnded && mbThreadTwoEnded) {
                clearInterval(mInterval); // just to be sure
                mbThreadOneEnded = false;
                mbThreadTwoEnded = false;
                alert("ThreadOne and ThreadTwo ended ");
            }
 
        }
 
 
 
 
 
    </script>
This entry was posted in Javascript css html and tagged , . Bookmark the permalink.


Comments are closed.

470 views