Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Any Stopwatch or Countup Timer function available?

I've tried searching around but have been unable to find a guide or sample code on how to create a stopwatch function or countup timer. I want to enable user to time the duration of a particular activity with start-stop buttons.

Any help will be greatly appreciated!

— asked November 7th 2010 by Julien Ho
  • android
  • count
  • countup
  • iphone
  • mobile
  • stopwatch
  • timer
0 Comments

3 Answers

  • Hi Julien

    If you are purely asking about the javascript logic you would use, what about something like this?

    In your start button event listener:

    var startTime = (new Date).getTime();
    

    Then attached to a stop button:

    var duration = (new Date).getTime() - startTime;
    

    If this isn't quite what you were asking, let me know.

    Hal

    — answered November 7th 2010 by Paul Dowsett
    permalink
    0 Comments
  • Hi julian …

    Take a peek at timers here under notes section…

    — answered November 8th 2010 by Satta Ravi
    permalink
    0 Comments
  • I am just starting with Appcelerator so I am not really sure about how to go about referencing variables. Can you reference them if they are set in a function. When I try to do this the Console message is "Can't find variable: startTime"; How do you use a variable in a function that is set in another function.

    Here are my two event listeners for the start and stop button:
    //add start event listener
    startButton.addEventListener('click', function(e){
    var startTime = (new Date).getSeconds();
    showLabel.text = startTime;
    });

    //add stop event listener
    stopButton.addEventListener('click', function(e){
    var endTime = (new Date).getSeconds() - startTime;
    showLabel.text = endTime;
    });

    Thanks in advance.

    — answered April 11th 2012 by Daniel Krajc
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.