<?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>Tortoise IT &#187; Javascript</title>
	<atom:link href="http://www.sean-barton.co.uk/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sean-barton.co.uk</link>
	<description>by Sean Barton, a freelance PHP website developer in Crewe, Cheshire</description>
	<lastBuildDate>Tue, 31 Jan 2012 17:34:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Live Countdown Timer using Javascript</title>
		<link>http://www.sean-barton.co.uk/2009/10/live-countdown-timer-using-javascript/</link>
		<comments>http://www.sean-barton.co.uk/2009/10/live-countdown-timer-using-javascript/#comments</comments>
		<pubDate>Sat, 10 Oct 2009 10:52:40 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Teaching]]></category>
		<category><![CDATA[Time Savers]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[timer]]></category>
		<category><![CDATA[Wordpress Plugins]]></category>

		<guid isPermaLink="false">http://www.sean-barton.co.uk/?p=350</guid>
		<description><![CDATA[Been done? Yes of course it has! However you don&#8217;t learn anything unless you get stuck in and have a go yourself and that is exactly what I did during an empty half hour period I had the other day (slow lecture). Whilst at university I am doing a lot of timed tasks (ten minutes etc.. as opposed to days) and the timers used are always a mobile phone timer or horrible Powerpoint countdown presentation. I wanted to write something which is simple to build on to create a JS version to ultimately integrate into online resources and lessons (Eg: &#8230; <a class="continue_reading" href="http://www.sean-barton.co.uk/2009/10/live-countdown-timer-using-javascript/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Been done? Yes of course it has! However you don&#8217;t learn anything unless you get stuck in and have a go yourself and that is exactly what I did during an empty half hour period I had the other day (slow lecture). Whilst at university I am doing a lot of timed tasks (ten minutes etc.. as opposed to days) and the timers used are always a mobile phone timer or horrible Powerpoint countdown presentation. I wanted to write something which is simple to build on to create a JS version to ultimately integrate into online resources and lessons (Eg: this task will take you ten minutes. Click here to turn on the timer&#8230;).</p>
<p>The  code at the bottom of this post is the raw HTML file I wrote. I obviously made it into a WordPress plugin as well. see below example.</p>
<p><strong>Example</strong></p>
<p>Yes, it&#8217;s basic but it works well for demonstrative purposes.</p>
<div id="countdown_body_wrapper">
                <form id="countdown_form" onsubmit="return do_countdown();">
                    Countdown from: <input style="width: 50px;" id="value" />
                    <select id="countdown_unit">
                        <option value="1">Seconds</option>
                        <option value="60">Minutes</option>
                        <option value="3600">Hours</option>
                    </select>
                    <input type="submit" value="Go" />
                </form>
                <div id="countdown_div"></div>
            </div>
<p><strong>Usage</strong></p>
<p>write sb_countdown in square brackets in a post or page and you will get the above example. If you want to edit the text or style it then just open the plugin file and right near the top is a few defines to get you going. I didn&#8217;t spend a lot of time on it so didn&#8217;t create an admin page. I may well do in the future though.</p>
<p><strong>The Download</strong></p>
<a class="downloadlink" href="http://www.sean-barton.co.uk/wp-content/plugins/download-monitor/download.php?id=15" title="Version 1 downloaded 540 times" >SB JS Countdown Timer (1.42 kB)</a>
<p><strong>Raw HTML Version</strong></p>
<pre>&lt;html&gt;
 &lt;head&gt;
 &lt;title&gt;Countdown Timer&lt;/title&gt;

 &lt;style&gt;
 body {
 font-family: tahoma;
 }

 #countdown_div {
 font-weight: bold;
 font-size: 56px;
 }

 #body_wrapper {
 padding: 10px;
 margin: 20px;
 }
 &lt;/style&gt;

 &lt;script&gt;
 function do_countdown() {
 var start_num = document.getElementById("value").value;
 var unit_var = document.getElementById("countdown_unit").value;

 start_num = start_num * parseInt(unit_var);

 var countdown_output = document.getElementById('countdown_div');

 if (start_num &gt; 0) {
 countdown_output.innerHTML = format_as_time(start_num);
 var t=setTimeout("update_clock(\"countdown_div\", "+start_num+")", 1000);
 }

 return false;
 }

 function update_clock(countdown_div, new_value) {
 var countdown_output = document.getElementById(countdown_div);
 var new_value = new_value - 1;

 if (new_value &gt; 0) {
 new_formatted_value = format_as_time(new_value);
 countdown_output.innerHTML = new_formatted_value;

 var t=setTimeout("update_clock(\"countdown_div\", "+new_value+")", 1000);
 } else {
 countdown_output.innerHTML = "And... Stop!";
 }
 }

 function format_as_time(seconds) {
 var minutes = parseInt(seconds/60);
 var seconds = seconds - (minutes*60);

 if (minutes &lt; 10) {
 minutes = "0"+minutes;
 }

 if (seconds &lt; 10) {
 seconds = "0"+seconds;
 }

 var return_var = minutes+':'+seconds;

 return return_var;
 }
 &lt;/script&gt;
 &lt;/head&gt;

 &lt;body&gt;
 &lt;div id="body_wrapper"&gt;
 &lt;form id="countdown_form" onsubmit="return do_countdown();"&gt;
 Countdown from: &lt;input style="width: 50px;" id="value" /&gt;
 &lt;select id="countdown_unit"&gt;
 &lt;option value="1"&gt;Seconds&lt;/option&gt;
 &lt;option value="60"&gt;Minutes&lt;/option&gt;
 &lt;option value="3600"&gt;Hours&lt;/option&gt;
 &lt;/select&gt;
 &lt;input type="submit" value="Go" /&gt;
 &lt;/form&gt;
 &lt;div id="countdown_div"&gt;&amp;nbsp;&lt;/div&gt;
 &lt;/div&gt;
 &lt;/body&gt;

&lt;/html&gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.sean-barton.co.uk/2009/10/live-countdown-timer-using-javascript/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Simple AJAX</title>
		<link>http://www.sean-barton.co.uk/2008/06/simple-ajax/</link>
		<comments>http://www.sean-barton.co.uk/2008/06/simple-ajax/#comments</comments>
		<pubDate>Tue, 24 Jun 2008 12:44:58 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.sean-barton.co.uk/?p=9</guid>
		<description><![CDATA[I&#8217;ve been teaching myself AJAX over the last month or so. I&#8217;ve written a simple version of the ever popular Autocomplete field but for now I want to give a quick example of what AJAX is and how it can be used simply and implemented quickly. What is AJAX? The acronym if I remember correctly stands for Asynchronous Javascript And XML but i&#8217;m likely to be wrong What you do need to know is that it uses Javascript to fire off custom requests wherever you want without having to reload the page. I have already mentionned the Autocomplete example above &#8230; <a class="continue_reading" href="http://www.sean-barton.co.uk/2008/06/simple-ajax/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been teaching myself AJAX over the last month or so. I&#8217;ve written a simple version of the ever popular Autocomplete field but for now I want to give a quick example of what AJAX is and how it can be used simply and implemented quickly.</p>
<p><strong>What is AJAX?</strong><br />
The acronym if I remember correctly stands for <strong>A</strong>synchronous <strong>J</strong>avascript <strong>A</strong>nd <strong>X</strong>ML but i&#8217;m likely to be wrong <img src='http://www.sean-barton.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>What you do need to know is that it uses Javascript to fire off custom requests wherever you want without having to reload the page. I have already mentionned the Autocomplete example above so I&#8217;ll use that as an example. At work we use something similar for a UK town search. A user starts to type in the name of a town and after a couple of keystrokes a list of suggestions appears.</p>
<p><strong>Now for the technical bit&#8230;<br />
</strong>In the above example, when the input field gets to a pre-determined number of characters the Javascript sends an asynchronous request to the server then does nothing. It does nothing because of course its asynchronous! The beauty of this is that the client machine doesn&#8217;t have to wait for a response so if a response comes then something will happen otherwise it fails seamlessly.</p>
<p>Putting this back into the example I gave, If the server returns something then the list of suggestions pops up or refreshes with the new data, otherwise it just wont do anything.</p>
<p>A callback function is declared and sent to the server as part of the request. If a number of conditions are met (ie: the server url is valid and it&#8217;s set up to receive such requests) then if the server echos anything it will be passed to the callback function. The callback then runs whatever code you have written and does what it wants with the returned result. This is better explained in some code so let&#8217;s see how it all works!</p>
<p>This script to use a remote server to echo a random number is the simplest thing i could think of:</p>
<pre>&lt;script&gt;

function ajax_request() {
	var request = false;

	if (navigator.appName == "Microsoft Internet Explorer") {
		request = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		request = new XMLHttpRequest();
	}

	request.abort();
	request.open("GET", "ajax.php", true);
	request.onreadystatechange=function() {

		if(request.readyState == 4) {
			alert(request.responseText);
		}
	}
	request.send(null);
}
&lt;/script&gt;</pre>
<p>Ill take you through it section by section&#8230;</p>
<p>First we need to create an AJAX request object, this is done is every proper browser using</p>
<pre>request = new XMLHttpRequest();</pre>
<p>Of course Microsoft have to be different and don&#8217;t support this call (at the time of this post) so we have to put in the nasty if statement that calls this code</p>
<pre>request = new ActiveXObject("Microsoft.XMLHTTP");</pre>
<p>Next we actually have to set up the request, ie: tell it where to call and what callback function to run</p>
<pre>request.open("GET", "ajax.php", true);
request.onreadystatechange=function() { }</pre>
<p>In this example we are telling it to call ajax.php using GET (POST is also an option) and to alert the response as the callback (alert(request.responseText);).<br />
Finally we need to fire off the request using</p>
<pre>request.send(null);</pre>
<p>There are a few bits i&#8217;ve missed out for the time being, namely</p>
<pre>if(request.readyState == 4) {

}</pre>
<p>This readyState option is checked in the callback and only does anything when it is  set to 4. This is the stage when a script actually does something. There are several other requests which i&#8217;m sure you can research but for the basic script, just leave the IF statement in. I also haven&#8217;t mentionned the XML side of things but we will leave that for another day.</p>
<p>The code that responds to all of this is a simple PHP script. It can simply echo out its data to be picked up by the callback as follows</p>
<pre>&lt;?php
echo rand(1,99);
?&gt;</pre>
<p>So that&#8217;s really it in terms of getting a basic example going but if you consider the possibilities of something rooted in something so simple then you will soon begin to see where you can easily place it into your script.</p>
<p>For example, the posting section in wordpress uses AJAX for an autosave feature in the post window. It probably has a bit of JS on a timer loop that takes the contents of the &#8216;Post&#8217; box and sends it to a script which updates a DB table and then returns the flag to say whether it saved properly or not. See! easy and very functional!</p>
<p>Alternatively if you like the idea of AJAX but can&#8217;t be bothered to play with the proper stuff then there&#8217;s always <a title="A JS Framework" href="http://jquery.com/" target="_blank">JQuery</a> and <a title="A JS Framework" href="http://script.aculo.us/" target="_blank">Scriptaculous</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sean-barton.co.uk/2008/06/simple-ajax/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced)
Database Caching 7/29 queries in 0.058 seconds using disk
Object Caching 388/413 objects using disk

Served from: www.sean-barton.co.uk @ 2012-02-05 13:43:32 -->
