Dark Bit Factory & Gravity
GENERAL => Challenges & Competitions => Topic started by: Jim on August 22, 2007
-
My very first ever javascript program. It's just a port, but it's let me know how it all works and what the limitations are ie. it's crap for any kind of computation!.
Jim
-
Loads of digits Jim. Nice one - works fine here on Mozilla :goodpost:
-
Very nice - accurate too.
-
My sinescrolls will look smoother than ever thanks to you! :)
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938
-
In Firefox I get, "A script on this page may be busy, or it may have stopped responding. You can stop the script now, or you can continue to see if the script will complete."
when I chose to let it continue the same message just pops up again after a while, pressing cancel shows what's presumably lots of digits of pi?
In IE the browser just crashes.
-
I wrote it in IE7 and it has the same problem about it being busy. I only let it do 900digits max, but that will take several minutes - it takes 5s in C.
I don't understand how the games framework p01 posted lets you run interactively without that problem.
Jim
-
Took some time for me too (firefox). I stopped the script after about 10 seconds, but still got about 100 digits of pie which is good enough for me :)
-
Jim: Unlike in C/C++, JS's main function are not supposed to be run in an (more or less) infinite loop. The browsers has other things to do, like handling I/O, rendering, ...
Therefore JS's "main", must be run a regular intervals using either setTimeout( functionHandle, delayInMs ) which will jump to the functionHandle once, or setInterval( functionHandle, delayInMs ) which will (do its best to) jump functionHandle every delayInMs ms.
Notice that to avoid consuming all the CPU resources, browser vendors allocate some time slots to the JS engine. Therefore if it's very small the delayInMs will not matter. The browser will execute the code at the next time slot. In practice 20ms is safe.
But within this 20ms, you nothing forbids you to compute several things.
-
I get a similar message in MSIE 6, but it's not serious - the code does what it is intended to do.
-
One more thing: document.write() is evil. It forces the DOM engine to parse what you inject. Prefer something like:
HTML<p id="output">3.</p>JSoutputTextHandle=document.getElementById('output').firstChild;
// ...
outputTextHandle.nodeValue += foo;
Changing only the nodeValue of a textNode is the method of choice as the browser knows you only touch a text node. No need to parse and append a chunk of markup.
HTH
Side note: combatking0, I hope the winners banner will read JavaScript comp, not Java comp ;)
-
Fortunately, Va!n has produced a wide selection of banners - some of which have the word "JavaScript" on them.
Thanks again, Va!n.
-
Thanks p01, K++, I think I get it...I know I'm being unfriendly when I want to take several minutes to render a page of html :D
I wrote a program that just did
var counter
main=function()
{document.write(counter++);
}
setInterval=(...whatever it was out of your example to call main..., 50)
Now, that I would expect would write out
0
1
2
3
but it doesn't. So I got something wrong and I probably need to post more code.
Does that setInterval cause a page refresh? If not, how does the document being rendered to get affected?
More to the point, I've read a load of docs that explain the syntax of js, and I've got that no problem - are there any good docs or sites that can tell me these other bits?
The great thing about this pi algorithm is it lends itself to this because it generates 9 digits at a time, and so it doesn't absoutely need to hog the cpu.
Jim
-
Jim: Like in C ( I guess, been a while since I last coded in C/C++ ), you must initialize counter before being able to increment it.
Neither setTimeout nor setInterval cause a refresh. That would be incredibly stupid, don't you think ;)
As I said document.write() is bad bad bad. Prefer the DOM way as I explained above and in the Updating text on a page (http://dbfinteractive.com/index.php?topic=1544.0) thread.
Here is a quick fix of your code snippet:
// boo ugly, no DOCTYPE !!!
<body>bla
<script>
var counter=0
main=function()
{
document.body.firstChild.nodeValue += counter++;
}
setInterval(main, 50)
</script>
</body>Regarding documentations, I'd say : go straight to the source a.k.a the W3C and ECMA, and check Enhancing Web Pages with JavaScript (http://www.slimeland.com/content/articles/jsenhance/) and the related links at bottom. And of course read some source codes ( FWIW mine might be a bit criptic but they can certainly help in a few areas )
HTH
-
In C, counter would be global and get initialised to 0 anyway, guaranteed by standard.
DOM is kind of odd - the whole point of javascript was to be really simple, then folks started hammering it and DOM had to be born.
When I was a kid, writing my first html page, doctype hadn't been invented! Or style sheets for that matter. I have a web page on geocities I wrote in 1995 using notepad.
I guess I'm looking for an overview and your links are very useful - thanks, and I see I have some reading to do :D My real problem is I've done nothing with web stuff since it was invented and so the problems, the solutions and the motivation behind them has passed me by.
Jim
-
When I was a kid, writing my first html page, doctype hadn't been invented! Or style sheets for that matter. I have a web page on geocities I wrote in 1995 using notepad.
I'm in the same boat!