JavaScript的新建"主题" [英] Javascript Create New "Thread"

查看:118
本文介绍了JavaScript的新建"主题"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个线程,根据JavaScript的,但code似乎并不适合传统的线程模型。

This is a "thread" according to javascript, but the code doesn't seem to fit the conventional threaded model.

是否有可能让这个code清晰的,关于线程的概念?

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 text asynchronously.

为了使code更清晰,你可以创建与做同样的一个有意义的名称功能:

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.

这篇关于JavaScript的新建"主题"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆