<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="btn">click1</div>
<input type="text" id="num">
</body>
</html>
<script>
var btn=document.getElementById("btn")
btn.onclick=function(){
var num=document.getElementById("num").value
var woker=new Worker('./woker.js')
woker.onmessage=function(event){
console.log("主线程接受数据"+event.data);
}
woker.postMessage(num)
}
</script>
worker.js
var onmessage=function(event){
console.log('此线程接受数据');
var upper=f(event.data)
postMessage(upper)
}
function f(n){
return n<=2 ? 1 : f(n-1)+f(n-2)
}
|