Solved

Javascript - Help with simple timer

Posted on 2014-02-15
2
286 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

777 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