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!
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
-
Hi julian …
Take a peek at timers here under notes section…
-
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.