Solved

Javascript - Help with simple timer

Posted on 2014-02-15
2
292 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

752 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