Rewriting my Javascript
February 6th, 2007 LukeA friend and I are busily sketching out a project idea that, among other things, involves a countdown. For a while, I couldn’t figure out how to do it. But after giving it a little bit more thought, I realized that all we’d need is a little bit of javascript and an initializing function that would begin the countdown, along with refreshing the page when the countdown was finished(most of the real ‘actions’ per se are run by server-side code, not client-side).
This was all well and good, and last night I happily sat down and hacked out a solution. You can see it in the portfolio section of girasquid.com. And it works, and it does what it needs to.
But at the same time, it’s not as … robust as I’d like it to be. It doesn’t do all the cool things. It won’t let me put more than one countdown on a page without hard-coding that. It won’t let me spawn off lots of countdowns at once with all different times, and then independently start and stop them.
For some of my projects, I encounter a problem like this and just go “oh. Well. I’ll do that later.”, and put it on my “torewrite” list(which is growing rather large, unfortunately). But this is a piece of code that’s generally useful, and I think that if I could develop it further so that it was perhaps object-oriented and more flexible, other people would use it. And that would be sweet. Therefore, I’m in the process of redesigning it. This is what I’ve sketched out.
We would have a Countdown object. It would have different member variables, that would make it easy for a separate script to interact with it’s parts. These are the variables and their breakdowns:
- itsHours - the number of hours remaining in the countdown.
- itsMinutes - the number of minutes remaining in the countdown.
- itsSeconds - the number of seconds remaining in the countdown.
- itsCounting(boolean) - whether or not the counter is actually moving or not
- itsRemainingSeconds - the number of seconds total(hours + minutes + seconds)
- itsRedirect - where to redirect the user to, once our countdown has finished(refresh/no action if empty)
- itsInterval - the interval we were using to continuously update our timer.
And then we would have different methods that we’d be able to call to actually do our counting. Here’s a sketch so far:
- initialize(seconds,redirect) - This creates a Countdown object for us, and sets the two member variables we’re supposed to be able to set.
- start() - this would actually start the countdown, along with setting itsCounting to true.
- stop() - this would stop the countdown at the second it was currently at, and set itsCounting to false.
There would also be multiple utility functions for things like making sure the hours/minutes display correctly and such. I’m planning on introducing one function that lets a user pass in a string with certain placeholders, and have the object return the string, with the placeholders filled in with their respective values. That would be cool.
I’m going to get to work on this.