robcowie / jquery-stopwatch

A jQuery plugin that renders a count-up clock from a defined start time
http://robcowie.github.com/jquery-stopwatch/
87 stars 31 forks source link

Added two new features: #5

Closed Antonio-Laguna closed 12 years ago

Antonio-Laguna commented 12 years ago

1 - Now you can choose a format. Pretty dirty but useful. Maybe I don't need to know the hour or I need to display the day (was commented). I don't know a prettier way to avoid the switch (I'm hating) 2 - Added a new method which let you retrieve the current time of the stopwatch.

robcowie commented 12 years ago

Sorry for the delay. I'm very busy with a work deadline at the moment. I'll get back to this in a few days.

Cheers, rob

Antonio-Laguna commented 12 years ago

Hey Rob, don't worry about that, despite the job, these are days to stay with family and friends :)

robcowie commented 12 years ago

Hi Belelros. I've finally had a look at this; Thanks again for the code. getTime() is straightforward so I'll add that as is. As for formatting, I've been thinking about it for a while;

After several aborted attempts at rolling my own implementation I discovered https://github.com/nene/jintervals, a nice little library that formats seconds in various ways. I have a dev branch which integrates it and allows users to provide a format string in the same way your code does. Nothing at all wrong with your work, but this way we get to benefit from the more full-featured jintervals lib. As an example:

$('').stopwatch('init', {format: '{Minutes} and {s.}'}).stopwatch('start');
    // 38 minutes and 38s

$('').stopwatch('init', {format: '{MM}:{SS}'}).stopwatch('start');
    //38:38

If jintervals is not available in the page, the default format of HH:MM:SS is used. The user can also completely override rendering by providing a callable to formatter in the options.

Any thoughts?

Antonio-Laguna commented 12 years ago

Hi @robcowie . It seems right and the jintervals library is pretty easy but powerfull. However, from my point of view, you should not rely on other plugins to do the work of yours. What I really mean, is that it's pretty odd (at least for me it would), to have to download another plugin in order to get stopwatch full-featured. Couldn't you add in a way that isn't necessary to force people to download both?

What do you think about it?

Also, I added a function in my local copy of stopwatch, which adds a function which is called when the timer reach some milliseconds. I'm using stopwatch for an internal pannel of my company to track how many time people is talking by phone, as a requirement, when they reach 4,5 minutes it should be some kind of alert on the pannel. So I added it too!

Do you think I should put it here too?

robcowie commented 12 years ago

I agree - sort of. It would be nice not to require third party code. On the other hand, it isn't uncommon; this code requires jquery. In other languages of course, it's considered a good thing to build on the efforts of other peoples libs.

It won't require jinternals; It will work without it but you'd lose the ability to format the output unless you were willing to provide your own formatting function.

I think there are four options:

  1. Implement time interval formatting in this lib (basically re-implement jintervals)
  2. Optionally use jintervals for formatting, defaulting to a fixed format if it isn't available
  3. Bundle jintervals in the lib; compress it and include in the same file.
  4. Don't provide any built-in formatting capabilities (well, except a default fixed format). Require that the user provides their own formatting function in which they use any third-party libs they choose.

As for the callback on elapsed time; It's a great idea. Push it to your fork and I'll take a look.

cheers.

Antonio-Laguna commented 12 years ago

Yes, you're right so.

I would go for two so. :D

I'm changing my fork to update that.

robcowie commented 12 years ago

Ok, cool. I'll push optional jintervals integration to the repo shortly with some updated tests and examples.