回答編集履歴
3
調整
test
CHANGED
@@ -35,7 +35,7 @@
|
|
35
35
|
console.log(e.data);
|
36
36
|
}
|
37
37
|
const func1=()=>new Promise(resolve=>setTimeout(()=>(console.log(1),resolve()),200));
|
38
|
-
const func2=()=>
|
38
|
+
const func2=()=>worker.postMessage(2);
|
39
39
|
const func3=()=>new Promise(resolve=>setTimeout(()=>(console.log(3),resolve()),100));
|
40
40
|
func1();
|
41
41
|
func2();
|
2
chousei
test
CHANGED
@@ -11,3 +11,34 @@
|
|
11
11
|
```
|
12
12
|
|
13
13
|
※ちなみに負荷の高い処理は命題で言う大量のループ処理はそのまま回すのではなくwokerで処理をするのが懸命です
|
14
|
+
|
15
|
+
# 参考
|
16
|
+
workerでの処理
|
17
|
+
```javascript
|
18
|
+
<script id="myWorker" type="javascript/worker">
|
19
|
+
const func=(n)=>{
|
20
|
+
const t=new Date().getTime();
|
21
|
+
const wait=300;
|
22
|
+
while(new Date().getTime()<t+wait){
|
23
|
+
void(0);
|
24
|
+
}
|
25
|
+
return n;
|
26
|
+
};
|
27
|
+
self.addEventListener('message',e=>{
|
28
|
+
self.postMessage(func(e.data));
|
29
|
+
});
|
30
|
+
</script>
|
31
|
+
<script>
|
32
|
+
const blob = new Blob([document.querySelector('#myWorker').textContent]);
|
33
|
+
const worker = new Worker(window.URL.createObjectURL(blob));
|
34
|
+
worker.onmessage=e=>{
|
35
|
+
console.log(e.data);
|
36
|
+
}
|
37
|
+
const func1=()=>new Promise(resolve=>setTimeout(()=>(console.log(1),resolve()),200));
|
38
|
+
const func2=()=>new Promise(resolve=>(worker.postMessage(2),resolve()));
|
39
|
+
const func3=()=>new Promise(resolve=>setTimeout(()=>(console.log(3),resolve()),100));
|
40
|
+
func1();
|
41
|
+
func2();
|
42
|
+
func3();
|
43
|
+
</script>
|
44
|
+
```
|
1
調整
test
CHANGED
@@ -9,3 +9,5 @@
|
|
9
9
|
func2();
|
10
10
|
func3();
|
11
11
|
```
|
12
|
+
|
13
|
+
※ちなみに負荷の高い処理は命題で言う大量のループ処理はそのまま回すのではなくwokerで処理をするのが懸命です
|