Solved

Javascript - Help with simple timer

Posted on 2014-02-15
2
293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 23

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…
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…
Suggested Courses

617 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