Created
December 5, 2010 04:59
-
-
Save rummelonp/728817 to your computer and use it in GitHub Desktop.
WebWorkerでDataURLが使えるか試してみた。使えなかった。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fib = function(n) { | |
if (n > 1) { | |
return fib(n-2) + fib(n-1); | |
} else { | |
return 1; | |
} | |
}; | |
window.addEventListener('load', function() { | |
var buttons = document.querySelectorAll('button'); | |
buttons = Array.prototype.slice.call(buttons); | |
buttons.forEach(function(e) { | |
e.addEventListener('click', function() { | |
e.blur(); | |
var target_id = e.getAttribute('target'); | |
var element = document.getElementById(target_id); | |
var js = element.value; | |
eval(js); | |
}, false); | |
}); | |
}, false); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<title>Web Worker Sample<title/> | |
<script src="application.js"></script> | |
</head> | |
<body> | |
<h1>Web Worker Sample</h1> | |
<section> | |
<h2>fibonacci function.</h2> | |
<section> | |
<pre> | |
var fib = function(n) { | |
if (n > 1) { | |
return fib(n-2) + fib(n-1); | |
} else { | |
return 1; | |
} | |
}; | |
</pre> | |
</section> | |
</section> | |
<section> | |
<h2>Worker js file.</h2> | |
<section> | |
<pre> | |
onmessage = function(e) { | |
postMessage(e.data.map(fib)); | |
}; | |
</pre> | |
</section> | |
</section> | |
<section> | |
<h2>#1 Worker with outher js file.</h2> | |
<textarea id="worker1" cols="100" rows="8">var data = [1,2,3,4,5,6,7,8,9,10,20,30]; | |
var worker = new Worker('worker.js'); | |
worker.onmessage = function(e) { | |
alert(e.data.join('\n')); | |
}; | |
worker.postMessage(data); | |
</textarea> | |
<button target="worker1">eval</button> | |
</section> | |
<section> | |
<h2>#2 Worker with data url.</h2> | |
<textarea id="worker2" cols="100" rows="10">var worker_js = 'data:text/javascript,var%20fib%20%3D%20function(n)%20%7B%0A%20%20if%20(n%20%3E%201)%20%7B%0A%20%20%20%20return%20fib(n-2)%20%2B%20fib(n-1)%3B%0A%20%20%7D%20else%20%7B%0A%20%20%20%20return%201%3B%0A%20%20%7D%0A%7D%3B%0A%0Aonmessage%20%3D%20function(e)%20%7B%0A%20%20postMessage(e.data.map(fib))%3B%0A%7D%3B'; | |
var data = [1,2,3,4,5,6,7,8,9,10,20,30]; | |
var worker = new Worker(worker_js); | |
worker.onmessage = function(e) { | |
alert(e.data.join('\n')); | |
}; | |
worker.postMessage(data); | |
</textarea> | |
<button target="worker2">eval</button> | |
</section> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fib = function(n) { | |
if (n > 1) { | |
return fib(n-2) + fib(n-1); | |
} else { | |
return 1; | |
} | |
}; | |
onmessage = function(e) { | |
postMessage(e.data.map(fib)); | |
}; |
bummer! Thanks for your feedback!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
"WebWorkerでDataURLが使えるか試してみた。使えなかった" means,
tried, but an exception occurred.