<?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>jquery &#8211; Luxing Huang</title>
	<atom:link href="https://luxing.im/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>https://luxing.im</link>
	<description>Thoughs and things</description>
	<lastBuildDate>Thu, 15 Sep 2016 22:01:47 +0000</lastBuildDate>
	<language>en-CA</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.2</generator>
<site xmlns="com-wordpress:feed-additions:1">58771605</site>	<item>
		<title>Using pingjs</title>
		<link>https://luxing.im/using-pingjs/</link>
					<comments>https://luxing.im/using-pingjs/#respond</comments>
		
		<dc:creator><![CDATA[Luxing Huang]]></dc:creator>
		<pubDate>Thu, 15 Sep 2016 21:58:55 +0000</pubDate>
				<category><![CDATA[Techie Stuff]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[pingjs]]></category>
		<category><![CDATA[selector]]></category>
		<guid isPermaLink="false">https://luxing.im/?p=690</guid>

					<description><![CDATA[Pingjs is a JavaScript library. It cleverly use the JavaScript&#8217;s native Image object to find the responsive time. Its example use has been provided by the author. The first &#8220;ping&#8221; to your target server is always incorrect. It counts the DNS resolve time in, hence I implemented this API to an endless loop of ping, &#8230; <p class="link-more"><a href="https://luxing.im/using-pingjs/" class="more-link">Continue reading<span class="screen-reader-text"> "Using pingjs"</span></a></p>]]></description>
										<content:encoded><![CDATA[<p><a href="https://github.com/jdfreder/pingjs" target="_blank">Pingjs</a> is a JavaScript library. It cleverly use the JavaScript&#8217;s native Image object to find the responsive time. Its example use has been provided by the author. The first &#8220;ping&#8221; to your target server is always incorrect. It counts the DNS resolve time in, hence I implemented this API to an endless loop of ping, to provide an accurate result back to the user.<br />
<span id="more-690"></span></p>
<p>The page I am generating will list all my servers in a table and there is a column called Ping with a button to trigger it. The column&#8217;s ID is generated based on the server&#8217;s hostname. For example:</p>
<pre>
<td id="ping_server1.example.com">
  <button type="button" onclick="runForever('server1.example.com')">Ping!</button>
</td>
</pre>
<p>Once the button in the <code>td</code> is clicked, the button will be replaced with the ping time in millisecond and automatically updated every 2 seconds.</p>
<p>When I was writing the function <code>runForever</code> I encountered a jQuery problem. In the selector, I am unable to select the id, e.g. <code>$("#ping_server1.example.com")</code>, and I figured that out it was a escaping problem. In the console provided by the browser&#8217;s web tool, I am able to modify the ID manually but not by js script.</p>
<pre>
// host is passed down in runForever
$("#ping_" + host.replace(/\./g, "\\\\.")).text(String(delta) + "ms");
</pre>
<p>The concatenation does not work, but manual type without concatenation does work! I have to use the oldschool way, see my final code:</p>
<pre>
$(document.getElementById('ping_' + host)).text(String(delta) + "ms");
</pre>
<p>The entire <code>runForever</code> function is:</p>
<pre>
function runForever(host)
{
    ping("http://" + host).then(function(delta) {
        $(document.getElementById('ping_' + host)).text(String(delta) + "ms");
    }).catch(function(err) {
        $(document.getElementById('ping_' + host)).text(err);
    });
    setTimeout(function() {runForever(host);}, 2000);
};
</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://luxing.im/using-pingjs/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">690</post-id>	</item>
	</channel>
</rss>
