<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" version="2.0">

<channel>
	<title>Living in Code</title>
	
	<link>http://blog.livingincode.com</link>
	<description />
	<lastBuildDate>Thu, 03 Nov 2011 16:50:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/livingincodeblog" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="livingincodeblog" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">livingincodeblog</feedburner:emailServiceId><feedburner:feedburnerHostname xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>Namespaces In JavaScript</title>
		<link>http://blog.livingincode.com/2011/10/namespaces-in-javascript/</link>
		<comments>http://blog.livingincode.com/2011/10/namespaces-in-javascript/#comments</comments>
		<pubDate>Sat, 22 Oct 2011 23:48:08 +0000</pubDate>
		<dc:creator>salil1011</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[javascript namespaces]]></category>

		<guid isPermaLink="false">http://blog.livingincode.com/?p=25</guid>
		<description><![CDATA[If you have programmed in languages like C++ or Java you should be familiar with the concept of namespaces and packages. If you are only familiar with JavaScript you probably might be wondering what these things are. Lets have a &#8230; <a href="http://blog.livingincode.com/2011/10/namespaces-in-javascript/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>If you have programmed in languages like C++ or Java you should be familiar with the concept of namespaces and packages. If you are only familiar with JavaScript you probably might be wondering what these things are. Lets have a look.</p>
<p>Alice needs to use a lengthy expression at several places in her code. To avoid repeating the expression in the code she decides to create a function for it. She names the function as <code>calculate</code>.</p>
<p><span id="more-25"></span></p>
<pre class="brush:js">function calculate(x, y) {
     return x*x + 2*x*y + y*y;
}</pre>
<p>Bob also needs to use some expression at several places in his code and so he too decides to create a function for it. He too names his function as <code>calculate</code>.</p>
<pre class="brush:js">function calculate(x, y) {
     return x*x + y*y;
}</pre>
<p>Both Alice and Bob have chosen the same name for their functions. Though the names of the functions are same each of them performs a different operation. Some day if they need to combine their codes, one of them will have to rename his function to prevent conflict.</p>
<p>To avoid such situations all programmers will have to communicate the names they use for their functions to every other programmer. Now that&#8217;s not convenient, is it? A better solution is to use namespaces. Namespaces would allow both Alice and Bob to use the same names for their functions and combine their codes without modification. Isn&#8217;t that great! Lets see how we can use namespaces in JavaScript.</p>
<p>Alice can define her namespace as shown below.</p>
<pre class="brush:js">var alice = {};</pre>
<p>Similarly Bob can define his own namespace.</p>
<pre class="brush:js">var bob = {};</pre>
<p>Alice and Bob can now define their <code>calculate</code> functions in their respective namespaces as shown below.</p>
<pre class="brush:js">alice.calculate = function(x, y) {
     return x*x + 2*x*y + y*y;
};

bob.calculate = function(x, y) {
     return x*x + y*y;
};</pre>
<p>A call can be made to their functions as:</p>
<pre class="brush:js">alice.calculate(10, 20); // This will call Alice's calculate function
bob.calculate(10, 20); // This will call Bob's calculate function</pre>
<p>This is how both Alice and Bob can use same names for their functions.<br />
Its not over yet! Bob can also use the same name <code>calculate</code> for two or more of his own functions. For this he has to define different sections in his namespace as shown below. </p>
<pre class="brush:js">var bob = {};
bob.project1 = {};
bob.project2 = {};</pre>
<p>Now Bob can define one <code>calculate</code> function under <code>bob.project1</code> and another <code>calculate</code> function under <code>bob.project2</code>.</p>
<p><strong>Some things to note:</strong></p>
<ul>
<li>JavaScript does not have namespaces! So what did we see now? When you write <code>var alice = {}</code> you actually create an object. With the help of objects we are just simulating namespace like behaviour found in other languages.</li>
<li>In real projects every programmer working in a project should not define his own namespace. The above example is provided for keeping things simple to understand. You generally create namespaces for grouping functions with similar functionality. You may read <a href="http://en.wikipedia.org/wiki/Java_package#Package_naming_conventions">how packages in Java are named</a> to get an idea of how you should choose names for your namespaces in JavaScript.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.livingincode.com/2011/10/namespaces-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript Functions With Variable Number Of Arguments</title>
		<link>http://blog.livingincode.com/2011/10/javascript-functions-with-variable-number-of-arguments/</link>
		<comments>http://blog.livingincode.com/2011/10/javascript-functions-with-variable-number-of-arguments/#comments</comments>
		<pubDate>Mon, 10 Oct 2011 18:43:48 +0000</pubDate>
		<dc:creator>salil1011</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[arguments]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[parameters]]></category>
		<category><![CDATA[polymorphism]]></category>

		<guid isPermaLink="false">http://blog.livingincode.com/?p=74</guid>
		<description><![CDATA[You design a function called sum which returns the sum of two numbers. Now you need a function which returns the sum of three numbers. So you define another function, say sumThree, which accepts three parameters and returns their sum. &#8230; <a href="http://blog.livingincode.com/2011/10/javascript-functions-with-variable-number-of-arguments/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>You design a function called <code>sum</code> which returns the sum of two numbers. Now you need a function which returns the sum of three numbers. So you define another function, say <code>sumThree</code>, which accepts three parameters and returns their sum. Later you need a function which returns the sum of four numbers. So you define a new function <code>sumFour</code>. What if your requirements keep on growing? How long will you keep adding new functions?</p>
<p>Worried? Thankfully you don&#8217;t need to do this. JavaScript allows you to define a single function which acts differently when different number of arguments are passed to it.<br />
<span id="more-74"></span><br />
Consider the below function.</p>
<pre class="brush:js">function sum(a, b) {
    return a + b;
}</pre>
<p>When the <code>sum</code> function is called as <code>sum(10, 20)</code> the value 10 is accessed from the function using the identifier <code>a</code> and the value 20 is accessed using the identifier <code>b</code>. Alternatively, the values 10 and 20 can be accessed from the function using the <code>arguments</code> array as shown below. The first parameter can be accessed using <code>arguments[0]</code>, the second one using <code>arguments[1]</code> and so on.</p>
<pre class="brush:js">function sum(a, b) {
    return arguments[0] + arguments[1];
}</pre>
<p>Before having a look at how we can write functions with variable number of arguments lets clear some misconceptions which some of you might have about JavaScript functions.</p>
<p>Normally you would call the above <code>sum</code> function as <code>sum(10, 20)</code> i.e. with two parameters. What would happen if you call it as <code>sum(10, 20, 30)</code>? If you think that it would cause an error then you are wrong! JavaScript does not check the parameter list in the function definition (formal parameters) with the parameter list in the function call (actual parameters). So the above <code>sum</code> function would simply ignore the third parameter. Because of this lenient nature of JavaScript it is possible to define the <code>sum</code> function without any formal parameters as shown below.</p>
<pre class="brush:js">function sum() {
    return arguments[0] + arguments[1];
}</pre>
<p>What would happen if you call the above <code>sum</code> function with a single parameter? Since <code>arguments[1]</code> would be undefined it would result in an error. You can pass more parameters than what the function is expecting but not less.</p>
<p>Now how would you modify the above function to calculate the sum of the first three parameters? You guessed it right! Simply include <code>arguments[2]</code> in the <code>return</code> statement. And here is how you would calculate the sum of all the arguments passed to the function.</p>
<pre class="brush:js">function sum() {
    var sum = 0;
    var i;
    for(i = 0; i < arguments.length; i++) {
        sum += arguments[i];
    }
    return sum;
}</pre>
<p>So now you have learned how to create functions with variable number of arguments. But the above example might give some of you a wrong notion that whenever you need to create functions with variable number of arguments you have to use the <code>arguments</code> array. Lets see why this notion is not correct.</p>
<p>Consider the below <code>calculate</code> function which accepts three parameters.</p>
<pre class="brush:js">function calculate(a, b, operation) {
    var result;
    if(operation == true) {
        result = a + b;
    } else {
        result = a * b;
    }
    return result;
}</pre>
<p>When the value of the <code>operation</code> parameter is <code>true</code> it returns the sum of the first two parameters and when the value of the <code>operation</code> parameter is <code>false</code> it returns the product of the first two parameters. Suppose you want the default value of the <code>operation</code> parameter to be <code>true</code>, that is, if no value is passed for the <code>operation</code> parameter the function should behave as if the value is true. One may write the below code to achieve this.</p>
<pre class="brush:js">function calculate() {
    var result;
    if(arguments.length == 2) {
        result = a + b;
    } else if(arguments.length == 3) {
        if(arguments[2] == true) {
            result = a + b;
        } else {
            result = a * b;
        }
    }
    return result;
}</pre>
<p>To check whether the <code>operation</code> parameter has been passed or not the above code checks the number of arguments passed. Depending on the number of arguments passed it behaves differently. Though it would give the desired result the code is not easy to read.<br />
The below code uses an alternative method to check whether the <code>operation</code> parameter has been passed or not.</p>
<pre class="brush:js">function calculate(a, b, operation) {
    var result;
    if(typeof operation == "undefined") {
        result = a + b;
    } else {
        result = a * b;
    }
    return result;
}</pre>
<p>Here the <code>typeof</code> operator is used to check whether the <code>operation</code> parameter has been passed or not. When it is not passed the <code>typeof</code> operator would result in the string <code>"undefined"</code>. This method makes the code more readable than the previous one. Below is a still better one.</p>
<pre class="brush:js">function calculate(a, b, operation) {
    var result;
    if(typeof operation == "undefined") {
        operation = true;
    }
    if(operation == true) {
        result = a + b;
    } else {
        result = a * b;
    }
    return result;
}</pre>
<p>Not much difference between the previous one and this one. Can you tell why this one is better?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.livingincode.com/2011/10/javascript-functions-with-variable-number-of-arguments/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Some Things You Should Know About JavaScript Arrays</title>
		<link>http://blog.livingincode.com/2011/10/some-things-you-should-know-about-javascript-arrays/</link>
		<comments>http://blog.livingincode.com/2011/10/some-things-you-should-know-about-javascript-arrays/#comments</comments>
		<pubDate>Wed, 05 Oct 2011 18:22:28 +0000</pubDate>
		<dc:creator>salil1011</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://livingincode.com/?p=12</guid>
		<description><![CDATA[JavaScript arrays are much more flexible than the arrays in languages like C and Java. Programmers who have used these languages and have just begun learning JavaScript assume JavaScript arrays to be similar to the ones in C and Java. &#8230; <a href="http://blog.livingincode.com/2011/10/some-things-you-should-know-about-javascript-arrays/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>JavaScript arrays are much more flexible than the arrays in languages like C and Java. Programmers who have used these languages and have just begun learning JavaScript assume JavaScript arrays to be similar to the ones in C and Java. These assumptions often result in inefficient code and sometimes even in incorrect code. This article discusses some of the not so well known concepts about JavaScript arrays.</p>
<p><span id="more-12"></span></p>
<h3>Shorthand for creating arrays</h3>
<p>You can create an array objects using the <code>Array</code> constructor. However, there is also a short and sweet method for achieving this. As shown in the below example, you can simply use a pair of square brackets to create an <code>Array</code> object instead of the <code>Array</code> constructor.</p>
<pre class="brush:js">// Using Array Constructor
var a = new Array();
var b = new Array("Apple", "Mango");

// Shorthand method
var a = [];
var b = ["Apple", "Mango"];
</pre>
<p>You can use the shorthand method most of the times. The only situation when you may need to use the <code>Array</code> constructor is when you want to specify the length of the array to be created. For example, <code>var a = new Array(10);</code></p>
<h3>A JavaScript array is not necessarily a collection of elements of the same data type</h3>
<p>Arrays in languages like C and Java are collections of elements of the same type. For example, an array of type <code>int</code> can only contain elements of type <code>int</code>. However, JavaScript is a loosely typed language. You do not declare the type of the array when creating it. You can store values of multiple data types in a single array. The array in the below example contains three elements all of different types.</p>
<pre class="brush:applescript">var myArray = [];

myArray[0] = 123;
myArray[1] = "Hello, world!";
myArray[2] = true;

alert(typeof myArray[0]);  // number
alert(typeof myArray[1]);  // string
alert(typeof myArray[2]);  // boolean
</pre>
<h3>For associative arrays, you don&#8217;t need to create <code>Array</code> objects</h3>
<p>Associative arrays are arrays in which strings are used as indexes of the elements.<br />
The point to note is that the inbuilt <code>Array</code> object does not handle associative arrays. See the below example.</p>
<pre class="brush:js">var capital = [];

capital["India"] = "Delhi";
capital["Sri Lanka"] = "Colombo";

alert(capital["India"]);     // Delhi
alert(capital["Sri Lanka"]); // Colombo
alert(capital.length);       // 0</pre>
<p>You can see that you can use strings as indexes for storing the values. But the <code>length</code> property of the array does not take these values into consideration. The <code>Array</code> object only acts on elements having indexes which are represented as integers. For example, <code>a[5]</code>, <code>a["5"]</code> and <code>a[5.0]</code> are one and the same and will be taken into account by the <code>Array</code> object. Whereas <code>a["hello"]</code>, <code>a["5a"]</code> and <code>a[5.23]</code> will not be taken into consideration.</p>
<p>The indexes (&#8220;India&#8221; and &#8220;Sri Lanka&#8221;) used in the previous example are not representations of integers. The <code>Array</code> object will not take them into account. Though creating an <code>Array</code> using <code>[]</code> works, it is unnecessary. The better way is to create a new <code>Object</code> instead of a new <code>Array</code> as shown below.</p>
<pre class="brush:js">var capital = {};            // Create new Object instead of new Array

capital["India"] = "Delhi";
capital["Sri Lanka"] = "Colombo";

alert(capital["India"]);     // Delhi
alert(capital["Sri Lanka"]); // Colombo</pre>
<h3>A note on the <code>length</code> property of the <code>Array</code> object</h3>
<p>The <code>length</code> property of the <code>Array</code> object returns <code>(largest index in the array + 1)</code>. See the below example.</p>
<pre class="brush:js">var a = [];

a[0] = 123;
a[4] = 456;
a["6"] = 789;      // 6 is the largest index
a["colour"] = "pink";

alert(a.length); // 7</pre>
<h3>Calculating the number of elements in an associative array</h3>
<p>An associative array is nothing but an object. The indexes are actually the properties of the object. So we can use the <code>for...in</code> loop for counting the number of elements as shown in the below code.</p>
<pre class="brush:js">var capital = {};

capital["India"] = "Delhi";
capital["Sri Lanka"] = "Colombo";
capital["China"] = "Beijing";

var count = 0;
for(i in capital) {
     count++;
}

alert(count); // 3</pre>
<p>Note that this counts all the elements in the array no matter whether the index is a string, integer or any other type.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.livingincode.com/2011/10/some-things-you-should-know-about-javascript-arrays/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introducing Delay In JavaScript Loops Using setTimeout And setInterval</title>
		<link>http://blog.livingincode.com/2011/10/introducing-delay-in-javascript-loops-using-settimeout-and-setinterval/</link>
		<comments>http://blog.livingincode.com/2011/10/introducing-delay-in-javascript-loops-using-settimeout-and-setinterval/#comments</comments>
		<pubDate>Sat, 01 Oct 2011 18:17:21 +0000</pubDate>
		<dc:creator>salil1011</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[delay]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[setInterval]]></category>
		<category><![CDATA[setTimeout]]></category>
		<category><![CDATA[sleep]]></category>

		<guid isPermaLink="false">http://livingincode.com/?p=8</guid>
		<description><![CDATA[JavaScript has two functions viz. setTimeout and setInterval which can be used to introduce delays in your program. But the method of using these functions is entirely different from what you might have used in other languages. People new to &#8230; <a href="http://blog.livingincode.com/2011/10/introducing-delay-in-javascript-loops-using-settimeout-and-setinterval/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>JavaScript has two functions viz. <code>setTimeout</code> and <code>setInterval</code> which can be used to introduce delays in your program. But the method of using these functions is entirely different from what you might have used in other languages. People new to programming may find this style quite awkward to use.</p>
<p>In this article we will see how we can introduce delays between the iterations of JavaScript loops.<br />
<span id="more-8"></span><br />
Consider a countdown script which counts from 10 to 0 with each value being displayed in a <code>p</code> tag on the HTML page. You can think of achieving this using a <code>for</code> loop or a <code>while</code> loop as below.</p>
<pre class="brush:js">var i = 10;
var counter = document.getElementById("counter");

while(i &gt;= 0){
     counter.innerHTML = i;
     i--;
}</pre>
<p>However, this would run too fast and we would only be able to see the final value (zero) on the HTML page. If we introduce a delay after every iteration of the loop we would be able to see all the values of the countdown. If you think that for adding the delay we will have to write some kind of code using <code>setTimeout</code> or <code>setInterval</code> after the <code>i--</code> statement,  then you are wrong. As mentioned in the beginning, the way of using <code>setTimeout</code> and <code>setInterval</code> is quite different. To introduce the delay, we will have to change the structure of the program completely.</p>
<h3>Using <code>setTimeout</code> to introduce delays</h3>
<p>First lets have a look at an example of <code>setTimeout</code> usage to understand how it works.</p>
<pre class="brush:js">setTimeout("alert('Shown after 2 seconds')", 2000);</pre>
<p>The first parameter is the code to be executed. The second parameter is the time (in microseconds) after which the code should be executed. In the example, the alert message will be shown after two seconds.</p>
<p>An alternative method is to pass the reference of a function instead of directly passing the code as a string.</p>
<pre class="brush:js">function showMessage(){
     alert("Shown after 2 seconds");
}
setTimeout(showMessage, 2000);</pre>
<p>Because of these ways of using the <code>setTimeout</code> function for introducing delays, we will have to change the structure of our countdown program completely. We will have to eliminate the while loop entirely and use functions instead. Here is how you do it.</p>
<pre class="brush:js">var i = 10;
var counter = document.getElementById("counter");

function countdown(){
     if(i &gt;= 0){
          counter.innerHTML = i;
          i--;
          setTimeout(countdown, 1000);
     }
}
countdown();</pre>
<p>If you understood the usage of the <code>setTimeout</code> function mentioned earlier, a little bit of thinking should give you an idea about how this code works.</p>
<p>What does this style of coding look like? You are right. It looks similar to the recursive style of implementing loops. But note that this is different from the recursion we use normally. In normal recursion, when a function calls itself, the call is synchronous. However, in this case the call to the <code>countdown</code> function using <code>setTimeout</code> is asynchronous.</p>
<h3>Using <code>setInterval</code> to introduce delays</h3>
<p>We can also implement our countdown program using <code>setInterval</code>. Lets look at the usage of <code>setInterval</code>.</p>
<pre class="brush:js">function doSomething(){
     // Some code
}
var timer = setInterval(doSomething, 2000);</pre>
<p>In the above example, the <code>doSomething</code> function will be executed every 2 seconds. The <code>setInterval</code> function returns a value which can be used to stop this repeated execution. To stop the repeated execution in the above example you can make a call to the <code>clearInterval</code> function as <code>clearInterval(timer)</code>;</p>
<p>Below is our countdown program implemented using <code>setInterval</code>.</p>
<pre class="brush:js">var i = 10;
var counter = document.getElementById("counter");
var timer;

function countdown(){
     if(i &gt;= 0){
          counter.innerHTML = i;
          i--;
     } else {
          clearInterval(timer);
     }
}
timer = setInterval(countdown, 1000);</pre>
<h3>This was too difficult for me to understand! Is there a simple way to introduce delays without getting into the details?</h3>
<p>The above methods might look quite awkward and new programmers might find them difficult to use. But if you compare the structure of the while loop code and the codes using <code>setTimeout</code> and <code>setInterval</code> you will notice that there is a one-to-one correspondence between the main parts of the two types of codes. Below I have shown the comparison of the while loop code and the <code>setTimeout</code> code. To convert any while loop code to <code>setTimeout</code> code, you just need to copy the INITIALISATION, CONDITION and LOOP BODY sections in the while loop code to their respective sections in the <code>setTimeout</code> code template and you are done. No need to worry about the details!</p>
<pre>// while loop code template
/* INITIALISATION */
while(/* CONDITION */){
     /* LOOP BODY */
}

// setTimeout code template
/* INITIALISATION */
function somename(){
     if(/* CONDITION */){
          /* LOOP BODY */
          setTimeout(somename, 100); // Change 100 to whatever
                                     // delay you require
     }
}
somename();</pre>
<p>Now try writing the template for <code>setInterval</code> code.</p>
<h3>Complete code listing of the <code>setTimeout</code> example</h3>
<pre class="brush:xml">&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN"
          "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html&gt;
&lt;head&gt;

&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
&lt;title&gt;Delay&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;p id="counter"&gt;&lt;/p&gt;
	&lt;script type="text/javascript"&gt;

	var i = 10;
	var counter = document.getElementById("counter");

	function countdown(){
	     if(i &gt;= 0){
	          counter.innerHTML = i;
	          i--;
	          setTimeout(countdown, 1000);
	     }
	}
	countdown();
	&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
<h3>Complete code listing of the <code>setInterval</code> example</h3>
<pre class="brush:xml">&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN"
          "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html&gt;

&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
&lt;title&gt;Delay&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;p id="counter"&gt;&lt;/p&gt;
	&lt;script type="text/javascript"&gt;

	var i = 10;
	var counter = document.getElementById("counter");
	var timer;

	function countdown(){
	     if(i &gt;= 0){
	          counter.innerHTML = i;
	          i--;
	     } else {
	          clearInterval(timer);
	     }
	}
	timer = setInterval(countdown, 1000);
	&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
<p>There is a slight difference between the functioning of the two examples. Can you spot it?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.livingincode.com/2011/10/introducing-delay-in-javascript-loops-using-settimeout-and-setinterval/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

