Javascript example: CubeTimer. Reviews?

Stefan

Legend
Joined
May 7, 2006
Messages
7,286
WCA ID
2003POCH01
YouTube
Visit Channel
I'm learning Javascript and wrote a minimalistic cube timer as exercise:
http://stefan-pochmann.info/ptimer/STimer.html

I'm pretty happy with the result and the code, but I'd appreciate if more experienced Javascript coders could have a look and tell me whether I'm doing something badly. Especially I'd like to know whether the time formatting can be done better, I really hate that part but couldn't find a better way.

I thought about asking in a Javascript forum and I still might, but I guess I'd have to explain some cube stuff to them. Also, maybe other cubers new to programming are interested in having a look at a timer with a manageable amount of code like this.
 
Im no coder, but I like the simplistic design. Now all you have to do is figure out averages, different puzzles, and cardboard stackmat functionality. I hope you figure out what ever problem you have right now, I cant really help.

And you kind of have to make it so that you have to press 2 buttons, or the spacebar to stop it. I found I can start and stop it by pressing only 1 button the keyboard.
 
Last edited:
It wasn't really a "lol this doesn't work" thing, it was more it's "standard" to put most/as much as possible of the javascript into the head.
 
And you kind of have to make it so that you have to press 2 buttons, or the spacebar to stop it. I found I can start and stop it by pressing only 1 button the keyboard.
You should be able to use the space bar. Does it not work?

Requiring two keys would make things more complicated and I wanted to keep this program minimalistic, hence the single-key usage.
 
And you kind of have to make it so that you have to press 2 buttons, or the spacebar to stop it. I found I can start and stop it by pressing only 1 button the keyboard.
You should be able to use the space bar. Does it not work?

Requiring two keys would make things more complicated and I wanted to keep this program minimalistic, hence the single-key usage.

Yes the space bar works.
 
This bit of code:
document.onkeydown = null;
window.setTimeout( 'document.onkeyup = startTimer', 1000 )​
is an interesting way to fix the timer from starting on keyup... I wouldn't have thought of that. Maybe try changing it so it disables the next onkeyup completely.
 
It wasn't really a "lol this doesn't work" thing, it was more it's "standard" to put most/as much as possible of the javascript into the head.
Ok, I see. Good practice. I changed it, is it good now?
http://stefan-pochmann.info/ptimer/STimer.html
(old: http://stefan-pochmann.info/ptimer/STimerOld.html)

This bit of code:
document.onkeydown = null;
window.setTimeout( 'document.onkeyup = startTimer', 1000 )​
is an interesting way to fix the timer from starting on keyup... I wouldn't have thought of that. Maybe try changing it so it disables the next onkeyup completely.
I think you might have misread. I disable onkeydown there, onkeyup already gets disabled when timing starts.

The idea is that always only one of them is active and they switch at start/stop of solve. Except the switch at stopping is delayed to prevent accidental restarting by hitting the key twice or hitting several keys. So for one second, neither keyup nor keydown are active.
 
It looks kinda like a very very early version of my timer :p I don't see any problems with the code although your scrambling code is quite opaque to me. (If the regular expressions are for replacing the 1 in your scrambles, I'd suggest ["","'","2"][Math.floor(3*Math.random())] instead of "12'".charAt(3*Math.random()). It's slightly longer but doesn't need to be corrected later.)
 
Except the switch at stopping is delayed to prevent accidental restarting by hitting the key twice or hitting several keys. So for one second, neither keyup nor keydown are active.
Ok, I think I did misunderstand. But a consequence of this is that once you have held down a key to stop the timer for over a second, it will start again once you pick that key up.
 
It looks kinda like a very very early version of my timer
I guess that's a good sign :)

your scrambling code is quite opaque to me.
Yeah well, sometimes I'm a bit of a short-code-geek. I don't think it's that bad, though, is it? I mean, I'm not even using ">>0" instead of Math.flloor().

I'd suggest ["","'","2"][Math.floor(3*Math.random())] instead of "12'".charAt(3*Math.random()).
Ah, thanks. Didn't know how to use arrays yet. It's actually shorter now. The other reason was to get rid of the space prefix. I'd like to just say ".trim()" but since that apparently doesn't exist (what the ****?!?), I just keep the space and threw out the replacing altogether.

But a consequence of this is that once you have held down a key to stop the timer for over a second, it will start again once you pick that key up.
True. But who does that?
 
Stefan, from your experience with computer solvers, would you consider it possible to implement a Javascript solver, capable on finding <25 move solutions in around 10 sec? And, would you consider using random-state scrambles in your timer?

Another feature which is missing from most (all?) web-based timers is a time breakdown feature. Blah's timer has a basic 2-stage one for BLD mode, but one with a user-defined number of stages would be much more useful to me, and probably many others.
 
I strongly prefer putting all JavaScript code in separate .js-files, separate from the HTML. They can be included with <script type="..." src="foo.js"></script>.

Code:
function startTimer ( event ) {
  ...
  key = event.which ? event.which : event.keyCode;
  ...
}

Here, key is a global variable. It stays alive after startTimer returns. To make it local (function scope), you have to use the var-keyword. You also use globals at a few other places similarly.

Code:
window.setTimeout( 'document.onkeyup = startTimer', 1000 );

This uses eval to execute the string. It can be avoided using a function instead of a string as the first argument. In this case it's not a big deal, but in general using functions instead of eval is better style. That way the code is checked at compile time.

If you plan to write something bigger, I *really* recommend learning and using JQuery or some other library. It hides all browser dependent things, like events, for you behind a nice interface. Debugging code that works fine in some browsers but not on others is incredibly annoying.

Edit:
Code:
timerId = window.setInterval( 'showTime()', 30 );

Much better this way:

Code:
timerId = window.setInterval( showTime, 30 );
 
Last edited:
would you consider it possible to implement a Javascript solver, capable on finding <25 move solutions in around 10 sec?
Maybe. Normal Thistlethwaite supposedly averages 31.3 moves:
http://www.jaapsch.net/puzzles/compcube.htm
But I think this just picks the first optimal solution for each of the four steps. Checking out more solutions like Cube Explorer does should lead to shorter results. How short, I don't know. But yeah, actually I intend to implement this, though maybe not in Javascript at first as I'm not yet comfortable enough with it.

And, would you consider using random-state scrambles in your timer?
Not in this one. This one's really just a little exercise for me and a demonstration for other Javascript/programmer beginners. I'm not going to extend this much. The idea is to support the functionality you see right now with the minimum amount of code.

I did try Herbert's Java Cube Explorer thing in my Java PTimer. It worked, but the first solve is a bit slow even on my relatively fast computer, so instead I wrote my own standard 25-moves generator. Maybe I'll put Herbert's stuff in there again, but another issue is that I'd have to find out how I'm allowed to do it.
 
Thanks for your suggestions, Johannes!

Separate .js file: Ok, I'll do that with larger projects. In this small one I still prefer it to be all in one file.

Global variables: Ah, so that's what "var" is for. Added a few now. Made my only local variable "scramble" global though so I can copy it to oldScramble simply from the variable rather than accessing the DOM.

setTimeout( 'document.onkeyup = startTimer', 1000 ): In this particular case I wouldn't want to have that simple statement somewhere else, separated from the preceding "document.onkeydown = null". But in general, especially for more code, I'll make an extra function. Didn't think of the compile-time checking advantage, but here the code is easy enough.

JQuery and other libraries: I'll definitely learn how to use them and do it. You're right, I was disappointed when I saw that even for checking the pressed key I have to look two ways. But this once, in this small program, I'm ok with it. I'd rather not include a library just for that.

setInterval( 'showTime()', 30 ) vs setInterval( showTime, 30 ): Ouch. I should've seen that.

Question: I hope the way I'm determining minutes and seconds works well. The idea is to separate them using only integers and only then divide and round the seconds. I was afraid that if I try to compute the minutes from the milliseconds by just dividing and rounding, I might get rounding errors and for example show "2" instead of "3" when it's like 2.999999999. What do you think about the way I did it, should that work like intended?
 
Back
Top