<?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>Moment.js &#8211; My Shitty Code</title>
	<atom:link href="https://myshittycode.com/tag/moment-js/feed/" rel="self" type="application/rss+xml" />
	<link>https://myshittycode.com</link>
	<description>Embracing the Messiness in Search of Epic Solutions</description>
	<lastBuildDate>Fri, 06 Jan 2023 17:23:12 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://myshittycode.com/wp-content/uploads/2022/04/cropped-icon-32x32.png</url>
	<title>Moment.js &#8211; My Shitty Code</title>
	<link>https://myshittycode.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">205304208</site>	<item>
		<title>Managing the Order of AJAX Calls on Input Field&#8217;s Keyup Event</title>
		<link>https://myshittycode.com/2013/09/19/managing-the-order-of-ajax-calls-on-input-fields-keyup-event/</link>
					<comments>https://myshittycode.com/2013/09/19/managing-the-order-of-ajax-calls-on-input-fields-keyup-event/#comments</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Thu, 19 Sep 2013 14:11:20 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[Moment.js]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=54</guid>

					<description><![CDATA[<p>SCENARIO Consider the following code:- When user types an employee&#8217;s name, &#8220;Mike&#8221;, in the search field, a web service call is fired per character typed. In this example, the following web service calls are made:- Let&#8217;s assume this web service searches the input string against databases (or flat files, Facebook API, etc) and returns a [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2013/09/19/managing-the-order-of-ajax-calls-on-input-fields-keyup-event/">Managing the Order of AJAX Calls on Input Field&#8217;s Keyup Event</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">SCENARIO</h2>



<p>Consider the following code:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
&#x5B;$(&#039;#employeeSearchField&#039;).keyup(function() {
	var query = $(this).val();

	$.get(&#039;/employees/api/search&#039;, { q: query }, function(data) {
		// do stuff
		...
	});

}).trigger(&#039;keyup&#039;);
</pre></div>


<p>When user types an employee&#8217;s name, &#8220;Mike&#8221;, in the search field, a web service call is fired per character typed. In this example, the following web service calls are made:-</p>



<ul class="wp-block-list">
<li>GET /employees/api/search?q=M</li>



<li>GET /employees/api/search?q=Mi</li>



<li>GET /employees/api/search?q=Mik</li>



<li>GET /employees/api/search?q=Mike</li>
</ul>



<p>Let&#8217;s assume this web service searches the input string against databases (or flat files, Facebook API, etc) and returns a list of employee JSON objects where their names match the given input string. The code above will take the result and display the employee list on the view.</p>



<h2 class="wp-block-heading">PROBLEM</h2>



<p>Since we can&#8217;t control how long each web service call takes to process the request, the order of the returned JSON objects might not match the order of the web service calls. As a result, we may have stale information being presented on the view. Further, we may have the annoying &#8220;flicker&#8221; problem where the old employee list overrides the new employee list on the view.</p>



<h2 class="wp-block-heading">SOLUTION</h2>



<p>To ensure the order of the returned JSON objects matches the order of the web service calls, we need to keep track each AJAX call&#8217;s timestamp. In this example, I&#8217;m using <a title="Moment.js" href="http://momentjs.com/" target="_blank" rel="noopener">Moment.js</a>, a date library, but you can also use the built-in <b>Date</b> object. For now, think of Moment.js as a Swiss Army Knife for date, or a Rambo Knife for date, or a MacGyver Knife for date&#8230; okay, maybe not MacGyver Knife since MacGyver can use a tooth pick to solve the date problem.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
// keep track latest AJAX call&#039;s timestamp
var latestAjaxCallDateTime;

$(&#039;#employeeSearchField&#039;).keyup(function() {
	var query = $(this).val();

	// before executing the AJAX call, store the latest timestamp
	latestAjaxCallDateTime = moment();

	// set the &quot;soon-to-be-executed&quot; AJAX call&#039;s timestamp to be the same
	// as the latest timestamp
	var currentAjaxCallDateTime = latestAjaxCallDateTime;

	$.get(&#039;/employees/api/search&#039;, { q: query }, function(data) {

		// if current timestamp is older than the latest timestamp, then
		// omit the request
		if (currentAjaxCallDateTime.isBefore(latestAjaxCallDateTime)) {
			return;
		}

		// do stuff
		...
	});

}).trigger(&#039;keyup&#039;);
</pre></div>


<p>Yes, this looks pretty hacky, but it works. The whole idea is we will not process the result if it is old. The <b>key</b> of making this whole thing work is to set <b>latestAjaxCallDateTime</b> as a global variable and set <b>currentAjaxCallDateTime</b> as a global variable WITHIN the anonymous function of the input field&#8217;s keyup event.</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2013/09/19/managing-the-order-of-ajax-calls-on-input-fields-keyup-event/">Managing the Order of AJAX Calls on Input Field&#8217;s Keyup Event</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2013/09/19/managing-the-order-of-ajax-calls-on-input-fields-keyup-event/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">54</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Lazy Loading (feed)
Database Caching using Disk

Served from: myshittycode.com @ 2026-02-21 00:10:21 by W3 Total Cache
-->