Solved

Javascript - Help with simple timer

Posted on 2014-02-15
2
280 Views
Last Modified: 2014-02-15
Hi.. I'm trying to create a simple timer .  But the on Stop function does not stop my timer.
Any ideas what is wrong. How can I create a simple timer with a start and stop button.
thx



  onStart: function ()
        {  
            var that = this;  
           
            _count = 0;
                   
           var _timer = setInterval(function()
           {
                if (_count == 60)
                {
                    _count = 0
                }
               
                _count++;
               
                that.set("counter", _count);
               
            },1000);
           
        },

        onStop: function () {
            var that = this;
           
           
            clearTimeout();
            clearInterval();      
           
           
            that.clearForm();          
        },

        clearForm: function () {
            var that = this;
            that.set("counter", "0");
        },
0
Comment
Question by:JElster
2 Comments
 

Assisted Solution

by:mokaid83
mokaid83 earned 250 total points
ID: 39861597
<html>
<head>
<script type="text/javascript">
var c=0;
var t;
var timer_is_on=0;
 
function timedCount()
{
document.getElementById('txt').value=c;
c=c+1;
t=setTimeout("timedCount()",1000);
}
 
function doTimer()
{
if (!timer_is_on)
  {
  timer_is_on=1;
  timedCount();
  }
}
 
function stopCount()
{
clearTimeout(t);
timer_is_on=0;
}
</script>
</head>
<body>
<form>
<input type="button" value="Start count!" onclick="doTimer()" />
<input type="text" id="txt" />
<input type="button" value="Stop count!" onclick="stopCount()" />
</form>
<p>
Click on the "Start count!" button above to start the timer. The input field will count forever, starting at 0. Click on the "Stop count!" button to stop the counting. Click on the "Start count!" button to start the timer again.
</p>
</body>
</html>
0
 
LVL 22

Accepted Solution

by:
Snarf0001 earned 250 total points
ID: 39861834
You just need to move your "_timer" variable outside the onstart function.
setInterval returns an id for that item (which you're capturing), but you have to pass that variable into clearInterval.

clearInterval(_timer);

So that variable needs to be outside the onStart function so that onStop can see and use it as well.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
populate dynamic drop down from a csv 3 39
Create a new template 9 21
Need sample Angular apps for study 4 35
Phone Dialer 5 36
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now