This 开发者_如何转开发is a "thread" according to javascript, but the code doesn't seem to fit the conventional threaded model.
Is it possible to make this code clearer, with regards to the concept of a thread?
function test() {
alert("Test");
}
// this creates a new "thread," but doesn't make much sense to the untrained eye
setTimeout(test, 0);
Is there some other way to branch off?
You are basically just taking the call to test
out of the normal flow and the engine will execute the function whenever it fits, as soon as possible. That means, you are executing test
asynchronously.
To make the code clearer, you could create a function with a meaningful name which does the same:
function executeAsync(func) {
setTimeout(func, 0);
}
executeAsync(function() {
alert("Test");
});
If you want to have real threads, have a look at web workers.
精彩评论