<?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>Tim Banks &#187; Javascript</title>
	<atom:link href="http://thetimbanks.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://thetimbanks.com</link>
	<description></description>
	<lastBuildDate>Mon, 20 Feb 2012 04:33:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Setting a threshold on jQuery ajax callbacks using prefilters</title>
		<link>http://thetimbanks.com/2011/06/08/setting-a-threshold-on-jquery-ajax-callbacks-using-prefilters/</link>
		<comments>http://thetimbanks.com/2011/06/08/setting-a-threshold-on-jquery-ajax-callbacks-using-prefilters/#comments</comments>
		<pubDate>Wed, 08 Jun 2011 14:42:53 +0000</pubDate>
		<dc:creator>Tim Banks</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[prefilter]]></category>

		<guid isPermaLink="false">http://thetimbanks.com/?p=178</guid>
		<description><![CDATA[This is a cross post from my company&#8217;s blog. Skip straight to the demo. When developing interfaces that include ajax functionality there will come a time when you will be showing some sort of loading animation and it will only show for a split second since the ajax call finished so fast.  This can cause the [...]]]></description>
			<content:encoded><![CDATA[<p>This is a cross post from my <a href="http://www.foliotek.com/devblog/setting-a-threshold-on-jquery-ajax-callbacks-using-prefilters/">company&#8217;s blog</a>.</p>
<div>
<p>Skip straight to the <a href="http://www.thetimbanks.com/demos/jquery/successThreshold/">demo</a>.</p>
<p>When developing interfaces that include ajax functionality there will come a time when you will be showing some sort of loading animation and it will only show for a split second since the ajax call finished so fast.  This can cause the user to be disoriented since they aren&#8217;t exactly sure what popped up.  It can be beneficial to slow down the interface so the user can see everything that is going on.</p>
<p>In jQuery 1.5, there is now a way to extend the $.ajax method.  There are three different ways to extend $.ajax: prefilters, converters, and transports.  I am going to use prefilters which the jQuery documentation describes as &#8220;generalized beforeSend callbacks to handle custom options or modify existing ones&#8221;.  I&#8217;m not going to go into details about what a prefilter is since the <a href="http://api.jquery.com/jQuery.ajaxPrefilter/">documentation</a> does a pretty good job.</p>
<p>Instead of setting up timeouts and clearing them out, I am wanting to pass a custom option to the ajax method.  Here is what I am trying to go for:</p>
<pre class="brush: jscript; title: ; notranslate">
$.ajax(url, {
    ...
    successThreshold: 3000,
    ...
})
</pre>
<p>The successThreshold option is my custom option.  The time passed into it will be the minimum amount of time it takes before the success callback gets called.  Now that I have my custom option, I can access it and modify the other options in my prefilter.</p>
<pre class="brush: jscript; title: ; notranslate">
$.ajaxPrefilter(function (options, originalOptions, jqXHR) {
    if (originalOptions.successThreshold &amp;amp;amp;&amp;amp;amp;  $.isFunction(originalOptions.success)) {
        var start, stop;
        options.beforeSend = function () {
            start = new Date().getTime();
            if ($.isFunction(originalOptions.beforeSend))
                originalOptions.beforeSend();
        };

        options.success = function (response) {
        var that = this, args = arguments;
        stop = new Date().getTime();

        function applySuccess() {
             originalOptions.success.apply(that, args);
        }

        var difference = originalOptions.successThreshold - (stop - start);
        if (difference &gt; 0)
            setTimeout(applySuccess, difference);
        else
            applySuccess();
        };
    }
});
</pre>
<p>The first thing I do in the prefilter is check to make sure both the successThreshold and success function are set.  I then override the beforeSend option in order to get the time before the ajax call starts.  In order to keep the success callback from firing before the threshold, the time difference needs to be calculated.  If the call didn&#8217;t take longer than the difference then set a timeout for the remaining time.  Otherwise just call the success callback immediately.</p>
<p>I have seen other solutions to this and all of them seem to set timeouts and clear them in different functions and it would have to be repeated for every call.  This can be defined in one place and used on any ajax call in the application.</p>
<p>I have added the code to my <a href="https://github.com/thetimbanks/successThreshold">Github repository</a>.  This <a href="http://www.thetimbanks.com/demos/jquery/successThreshold/">demo</a> shows the code in action.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://thetimbanks.com/2011/06/08/setting-a-threshold-on-jquery-ajax-callbacks-using-prefilters/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery Extension: toggleText() method</title>
		<link>http://thetimbanks.com/2011/03/22/jquery-extension-toggletext-method/</link>
		<comments>http://thetimbanks.com/2011/03/22/jquery-extension-toggletext-method/#comments</comments>
		<pubDate>Tue, 22 Mar 2011 12:48:45 +0000</pubDate>
		<dc:creator>Tim Banks</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[jquery extension]]></category>
		<category><![CDATA[jquery plugin]]></category>
		<category><![CDATA[toggle]]></category>
		<category><![CDATA[toggletext]]></category>

		<guid isPermaLink="false">http://thetimbanks.com/?p=153</guid>
		<description><![CDATA[Skip straight to the demo. Here&#8217;s a situation I have run into multiple times: I have a hidden area that is toggled by a link.  The link will usually say &#8220;Show *&#8221;, and once it is clicked it will say &#8220;Hide *&#8221;.  I would usually just check the text when the link is clicked and [...]]]></description>
			<content:encoded><![CDATA[<p>Skip straight to the <a href="http://thetimbanks.com/demos/jquery/extensions/toggleText/">demo</a>.</p>
<p>Here&#8217;s a situation I have run into multiple times: I have a hidden area that is toggled by a link.  The link will usually say &#8220;Show *&#8221;, and once it is clicked it will say &#8220;Hide *&#8221;.  I would usually just check the text when the link is clicked and switch it.  Similar to the following:</p>
<pre class="brush: jscript; title: ; notranslate">
$(&quot;a&quot;).click(function() {
    var $self = $(this);
    if ($self.text() == &quot;Show items&quot;)
       $self.text(&quot;Hide items&quot;);
    else
        $self.text(&quot;Show items&quot;);
});
</pre>
<p>It would get tedious to add this to every link that toggled the view on an element.  In order to make it easier on myself I threw together a little extension for jQuery.  You pass it 2 string values representing the text that you would like to toggle.  Here is an example usage that does the same thing as the code block above:</p>
<pre class="brush: jscript; title: ; notranslate">
$(&quot;a&quot;).click(function() {
    $(this).toggleText(&quot;Show&quot;, &quot;Hide&quot;);
});
</pre>
<p>The extension looks for the first value in the element&#8217;s text and if it exists, it is replaced with the second value.  If the first value doesn&#8217;t exist in the element&#8217;s text, it looks for the second value in the text and replaces it with the first value.   Here is the source:</p>
<pre class="brush: jscript; title: ; notranslate">
jQuery.fn.toggleText = function (value1, value2) {
    return this.each(function () {
        var $this = $(this),
            text = $this.text();

        if (text.indexOf(value1) &gt; -1)
            $this.text(text.replace(value1, value2));
        else
            $this.text(text.replace(value2, value1));
    });
};
</pre>
<p>To see it in action, you can <a href="http://thetimbanks.com/demos/jquery/extensions/toggleText/">view the demo</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://thetimbanks.com/2011/03/22/jquery-extension-toggletext-method/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using jQuery and YQL to get an RSS feed from a site</title>
		<link>http://thetimbanks.com/2010/08/03/using-jquery-and-yql-to-get-an-rss-feed-from-a-site/</link>
		<comments>http://thetimbanks.com/2010/08/03/using-jquery-and-yql-to-get-an-rss-feed-from-a-site/#comments</comments>
		<pubDate>Tue, 03 Aug 2010 16:18:00 +0000</pubDate>
		<dc:creator>Tim Banks</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[RSS]]></category>
		<category><![CDATA[YQL]]></category>

		<guid isPermaLink="false">http://thetimbanks.com/?p=81</guid>
		<description><![CDATA[I forgot to post a link to this earlier, but I wrote a blog post on using jQuery and YQL to get an RSS feed from a web site. The code uses YQL to grab the content of the external site and then jQuery to find a link tag pointing to the location of the [...]]]></description>
			<content:encoded><![CDATA[<p>I forgot to post a link to this earlier, but I wrote a blog post on using jQuery and YQL to get an RSS feed from a web site.  The code uses YQL to grab the content of the external site and then jQuery to find a link tag pointing to the location of the RSS feed.  This is similar to how Firefox displays the RSS feed icon in the address bar.</p>
<p><a href="http://lanitdev.wordpress.com/2010/02/26/using-jquery-and-yql-to-get-an-rss-feed-from-a-site/">Check out the full post here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://thetimbanks.com/2010/08/03/using-jquery-and-yql-to-get-an-rss-feed-from-a-site/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery Custom Selector for selecting elements by exact text :textEquals</title>
		<link>http://thetimbanks.com/2009/09/22/jquery-custom-selector-for-selecting-elements-by-exact-text-textequals/</link>
		<comments>http://thetimbanks.com/2009/09/22/jquery-custom-selector-for-selecting-elements-by-exact-text-textequals/#comments</comments>
		<pubDate>Tue, 22 Sep 2009 13:52:55 +0000</pubDate>
		<dc:creator>Tim Banks</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://thetimbanks.com/?p=15</guid>
		<description><![CDATA[I just finished up writing another short blog post over on my company&#8217;s development blog covering a jQuery custom selector I wrote to find elements based on their exact text.  There was a particular case where I needed to select elements in jQuery based on their exact text and not on whether the text contained [...]]]></description>
			<content:encoded><![CDATA[<p>I just finished up writing another short blog post over on my company&#8217;s development blog covering a jQuery custom selector I wrote to find elements based on their exact text.  There was a particular case where I needed to select elements in jQuery based on their exact text and not on whether the text contained a certain string.  That ruled out the possibility to use the built in contains() method.</p>
<p><a href="http://lanitdev.wordpress.com/2009/09/22/jquery-custom-selector-for-selecting-elements-by-exact-text-textequals/">Lanit Development Blog &#8211; jQuery Custom Selector for selecting elements by exact text :textEquals</a></p>
]]></content:encoded>
			<wfw:commentRss>http://thetimbanks.com/2009/09/22/jquery-custom-selector-for-selecting-elements-by-exact-text-textequals/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

