<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tutorials Archive &#187; OOP</title>
	<atom:link href="http://tutarchive.com/tag/oop/feed/" rel="self" type="application/rss+xml" />
	<link>http://tutarchive.com</link>
	<description>Save and Share your Tutorials</description>
	<lastBuildDate>Thu, 04 Feb 2010 02:51:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Object Oriented Programming with JavaScript : Timer Class</title>
		<link>http://tutarchive.com/2009/09/object-oriented-programming-with-javascript-timer-class/</link>
		<comments>http://tutarchive.com/2009/09/object-oriented-programming-with-javascript-timer-class/#comments</comments>
		<pubDate>Tue, 22 Sep 2009 02:06:23 +0000</pubDate>
		<dc:creator>TutArchive</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Object Oriented Programming]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://tutarchive.com/?p=222</guid>
		<description><![CDATA[JavaScript with Oops is not much used by developers while creating application however we can make our life easy if we use JavaScript with Oops.
Let's started object oriented programming in JavaScript by taking example of a Timer class, similar to the Timer control in windows application.
Create Class
Create the class Timer. Below code shows how to [...]


Related posts:<ol><li><a href='http://tutarchive.com/2009/09/flex-alert-listen-to-the-buttons/' rel='bookmark' title='Permanent Link: Flex Alert! Listen to the buttons'>Flex Alert! Listen to the buttons</a></li><li><a href='http://tutarchive.com/2009/09/checking-username-availability-with-mootools-and-request-json/' rel='bookmark' title='Permanent Link: Checking Username Availability with Mootools and Request.JSON'>Checking Username Availability with Mootools and Request.JSON</a></li></ol>]]></description>
			<content:encoded><![CDATA[<p>JavaScript with Oops is not much used by developers while creating application however we can make our life easy if we use JavaScript with Oops.</p>
<p>Let's started object oriented programming in JavaScript by taking example of a Timer class, similar to the Timer control in windows application.<br />
<strong>Create Class</strong></p>
<p>Create the class Timer. Below code shows how to declare the class</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Declaring class &quot;Timer&quot;</span>
<span style="color: #003366; font-weight: bold;">var</span> Timer <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// ....</span>
    <span style="color: #006600; font-style: italic;">// Class body</span>
    <span style="color: #006600; font-style: italic;">// ....</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><span id="more-222"></span><br />
<strong>Add Public Properties</strong></p>
<p>To add public properties to the class we use this keyword as shown below</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Declaring class &quot;Timer&quot;</span>
<span style="color: #003366; font-weight: bold;">var</span> Timer <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Property: Frequency of elapse event of the timer in millisecond</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">Interval</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Property: Whether the timer is enable or not</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">Enable</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Boolean<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You can see that we have created 2 properties (Interval and Enable) of Timer class and assigned default values to them.<br />
<strong>Add Events (If any)</strong></p>
<p>Now we might want to add events to the class in case we need. For Timer class we need "Tick" event which will be fired every after "Interval". Event are added the same way as the public properties.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Declaring class &quot;Timer&quot;</span>
<span style="color: #003366; font-weight: bold;">var</span> Timer <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Property: Frequency of elapse event of the timer in millisecond</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">Interval</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Property: Whether the timer is enable or not</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">Enable</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Boolean<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Event: Timer tick</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">Tick</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Add Private Member Variables</strong><br />
We need 2 private variable in the class to hold the timer Id and the class instance. The private variable are added using 'var' keyword.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Declaring class &quot;Timer&quot;</span>
<span style="color: #003366; font-weight: bold;">var</span> Timer <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Property: Frequency of elapse event of the timer in millisecond</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">Interval</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Property: Whether the timer is enable or not</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">Enable</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Boolean<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Event: Timer tick</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">Tick</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Member variable: Hold interval id of the timer</span>
    <span style="color: #003366; font-weight: bold;">var</span> timerId <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Member variable: Hold instance of this class</span>
    <span style="color: #003366; font-weight: bold;">var</span> thisObject<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Add Functions</strong><br />
Public functions are also added the same way as public properties using this keyword. We need 2 function in the timer, first "Start" to start the timer and second "Stop" to stop the timer.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Declaring class &quot;Timer&quot;</span>
<span style="color: #003366; font-weight: bold;">var</span> Timer <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Property: Frequency of elapse event of the timer in millisecond</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">Interval</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Property: Whether the timer is enable or not</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">Enable</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Boolean<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Event: Timer tick</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">Tick</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Member variable: Hold interval id of the timer</span>
    <span style="color: #003366; font-weight: bold;">var</span> timerId <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Member variable: Hold instance of this class</span>
    <span style="color: #003366; font-weight: bold;">var</span> thisObject<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Function: Start the timer</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">Start</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">Enable</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Boolean<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        thisObject <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>thisObject.<span style="color: #660066;">Enable</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            thisObject.<span style="color: #660066;">timerId</span> <span style="color: #339933;">=</span> setInterval<span style="color: #009900;">&#40;</span>
            <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                thisObject.<span style="color: #660066;">Tick</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> thisObject.<span style="color: #660066;">Interval</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Function: Stops the timer</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">Stop</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        thisObject.<span style="color: #660066;">Enable</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Boolean<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        clearInterval<span style="color: #009900;">&#40;</span>thisObject.<span style="color: #660066;">timerId</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You can see I have incorporated the timer functionality using the setInterval function of the JavaScript. Our class is ready for use now.</p>
<p><strong>Using the Class</strong><br />
To use the class first create the instance of Timer.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Timer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now assign the value to its properties as needed.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">obj.<span style="color: #660066;">Interval</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">300</span><span style="color: #339933;">;</span></pre></div></div>

<p>Attach handler to the events</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">obj.<span style="color: #660066;">Tick</span> <span style="color: #339933;">=</span> timer_tick<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> timer_tick<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Do something..</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now call the "Start" function to start the timer</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">obj.<span style="color: #660066;">Start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>To stop the timer anytime in between just call the "Stop" function.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">obj.<span style="color: #000066;">Stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p style="padding-left: 30px; text-align: right;">Source: <a href="http://www.dailycoding.com/Posts/object_oriented_programming_with_javascript__timer_class.aspx" target="_blank">Dailycoding</a></p>


<p>Related posts:<ol><li><a href='http://tutarchive.com/2009/09/flex-alert-listen-to-the-buttons/' rel='bookmark' title='Permanent Link: Flex Alert! Listen to the buttons'>Flex Alert! Listen to the buttons</a></li><li><a href='http://tutarchive.com/2009/09/checking-username-availability-with-mootools-and-request-json/' rel='bookmark' title='Permanent Link: Checking Username Availability with Mootools and Request.JSON'>Checking Username Availability with Mootools and Request.JSON</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://tutarchive.com/2009/09/object-oriented-programming-with-javascript-timer-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
