How to Simulate Threads in JavaScript

Hello, Today I’m going to explain how you can simulate threads in JavaScript. Because JavaScript doesn’t have an explicit thread object, like C# or Java does. But it does have 2(two) methods (setTimeout and setInterval) that you can use in order to get at least some of the basic behaviors of threads.

You don’t have all the features like: sleep, suspend, resume and some others, but you can specify when to start and end it, with just these 2(two) I guaranty you can do a lot.

You might ask why use threads or even why do I need something like that for a web application, well, we know that nowadays the users have better computers that can handle more process, but we cannot count on that and have to consider that a big process can freeze the user’s interface, which for sure will frustrate them.

That’s exactly when threads come in handy, when you have a task that might take a long time to finish and doing that could freeze the user’s interface. So what you need is somehow put this long process in another thread, which will not interfere with the thread that is taking care of the user’s interface.

In the 2(two) videos below I will show how you can accomplish that.

Thank you for your time and feel free to leave any comments or questions.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.