<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shafiqul Islam Shuvo</title>
    <description>The latest articles on DEV Community by Shafiqul Islam Shuvo (@meghsohor).</description>
    <link>https://dev.to/meghsohor</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F195373%2F36301f4b-7f1a-438b-bd2b-3ded5554682e.jpg</url>
      <title>DEV Community: Shafiqul Islam Shuvo</title>
      <link>https://dev.to/meghsohor</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/meghsohor"/>
    <language>en</language>
    <item>
      <title>Modern Browser APIs - Part 1</title>
      <dc:creator>Shafiqul Islam Shuvo</dc:creator>
      <pubDate>Sat, 03 Oct 2020 05:22:34 +0000</pubDate>
      <link>https://dev.to/meghsohor/modern-browser-apis-part-1-3noa</link>
      <guid>https://dev.to/meghsohor/modern-browser-apis-part-1-3noa</guid>
      <description>&lt;p&gt;The modern browsers have created a handful of APIs which can help the developers to build applications that can perform better, work offline, and provide a better user experience. Today we are going to check &lt;strong&gt;5 APIs&lt;/strong&gt; of Modern Browsers that we can use for web applications development and save ourselves from a lot works and troubles:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Window.requestAnimationFrame&lt;/li&gt;
&lt;li&gt;Resource Prefetch&lt;/li&gt;
&lt;li&gt;Resource Preload&lt;/li&gt;
&lt;li&gt;Navigator.sendBeacon&lt;/li&gt;
&lt;li&gt;Intersection Observer&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Window.requestAnimationFrame
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Window.requestAnimationFrame()&lt;/code&gt; api can be used to call a function every time display frame changes/updates which is approximately 60 per second or more. Using this method we can create a smooth animation or make a polling function.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;requestAnimationFrame&lt;/code&gt; has the following benefits:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Browser has the ability to optimize the performance&lt;/li&gt;
&lt;li&gt;Execution will be stopped for the inactive browser tabs&lt;/li&gt;
&lt;li&gt;More accurate than setTimeout/setInterval &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Demo&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;animate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="c1"&gt;// your code goes here&lt;/span&gt;

  &lt;span class="c1"&gt;//the will be called repeatedly by requestAnimationFrame&lt;/span&gt;
  &lt;span class="nx"&gt;requestAnimationFrame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;animate&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// Intial call to the function&lt;/span&gt;
&lt;span class="nx"&gt;animate&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We also can stop animation using the &lt;code&gt;window.cancelAnimationFrame&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cancelAnimationFrame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;animate&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A polling function to &lt;strong&gt;wait for an HTML element&lt;/strong&gt; using &lt;code&gt;requestAnimationFrame&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;waitForElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;elem&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;elem&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;string&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;wfelem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;elem&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;elem&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;requestAnimationFrame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;wfelem&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;};&lt;/span&gt;
            &lt;span class="nx"&gt;wfelem&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// You can use the function in following way:&lt;/span&gt;

&lt;span class="nx"&gt;waitForElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;body&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;background&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;h3&gt;
  
  
  Resource Prefetch
&lt;/h3&gt;

&lt;p&gt;Resource prefetching is a technique that we can use to tell the browser to download a page or resource even before the users are going to access them. This technique can be used for the pages that the users are most likely going to visit. The browser will download and cache the resource in the background with a low priority, so it won't interfere with more important resources.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"prefetch"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"/your-resource-link"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;h3&gt;
  
  
  Resource Preload
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Resource preloading&lt;/strong&gt; is similar to &lt;strong&gt;Resource prefetching&lt;/strong&gt; but the difference is that &lt;strong&gt;Prefetching&lt;/strong&gt; is a way to tell the browser what resource to download that likely to be accessed on the next navigation and &lt;strong&gt;Preloading&lt;/strong&gt; is to load a resource that will be needed for the current page. The syntax is similar but we also need to provide the resource type in &lt;code&gt;as&lt;/code&gt; attribute.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"preload"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"/images/background.jpg"&lt;/span&gt; &lt;span class="na"&gt;as=&lt;/span&gt;&lt;span class="s"&gt;"image"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;h3&gt;
  
  
  Navigator.sendBeacon
&lt;/h3&gt;

&lt;p&gt;Sometimes we may need to send data to server when a visitor leaves a page. When a user leaves a page, JavaScript fires an event called &lt;code&gt;unload&lt;/code&gt; and it is not an easy task to send an Ajax request during this event. &lt;br&gt;
We can use &lt;code&gt;XMLHttpRequest&lt;/code&gt;  in an  &lt;code&gt;unload&lt;/code&gt;  handler. But it will prevent the browser to load the next page until the request is completed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;unload&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;xhr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;XMLHttpRequest&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nx"&gt;xhr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/log&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// third parameter of `false` means synchronous&lt;/span&gt;
  &lt;span class="nx"&gt;xhr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But &lt;code&gt;Navigator.sendBeacon()&lt;/code&gt; is here to solve that problem. This method is useful when you need to send a data through &lt;strong&gt;Ajax&lt;/strong&gt; but don't expect a response. And the request is executed asynchronously when the browser gets a chance to send the data during the idle processing time.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Navigator.sendBeacon()&lt;/code&gt; takes two parameters: first one is the &lt;strong&gt;url&lt;/strong&gt; and second one is the &lt;strong&gt;data&lt;/strong&gt;. The function will return a &lt;code&gt;true&lt;/code&gt; response when the data is successfully sent to server or &lt;code&gt;false&lt;/code&gt; if the request isn't successful.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;unload&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sendBeacon&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// Send the beacon&lt;/span&gt;
      &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sendBeacon&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="c1"&gt;// Log the result&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;h3&gt;
  
  
  Intersection Observer
&lt;/h3&gt;

&lt;p&gt;It is hard to find a Developer who didn't write/use a code to check if &lt;strong&gt;an element is visible in the viewport&lt;/strong&gt;.&lt;br&gt;
We usually create a function and attach it to the &lt;code&gt;scroll&lt;/code&gt; event and check if the target element pops up in the screen. Check the following snippet in &lt;strong&gt;jQuery&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;scroll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;top_of_element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#element&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;top&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;bottom_of_element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#element&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;top&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#element&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;outerHeight&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;bottom_of_screen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;scrollTop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;innerHeight&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;top_of_screen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;scrollTop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;bottom_of_screen&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;top_of_element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;top_of_screen&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;bottom_of_element&lt;/span&gt;&lt;span class="p"&gt;)){&lt;/span&gt;
        &lt;span class="c1"&gt;// the element is visible, do something&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// the element is not visible, do something else&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Whew!&lt;/em&gt;&lt;/strong&gt; Lots of calculations is going on there which I have never been able to remember. But, we now have &lt;code&gt;IntersectionObserver()&lt;/code&gt; method which can save us from all of these troubles.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;observer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;IntersectionObserver&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;elements&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;elements&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelectorAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;p&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first argument of &lt;code&gt;IntersectionObserver&lt;/code&gt; is a function that can take an array of HTML elements. There is a second optional argument that takes an object of options:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;observer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;IntersectionObserver&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;elements&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;elements&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;intersectionRatio&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;intersectionRatio&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;opacity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;0.5&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;intersectionRatio&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;opacity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// You can use a single value&lt;/span&gt;
    &lt;span class="na"&gt;threshold&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt; &lt;span class="c1"&gt;// The observer's callback will be once the target element is 50% visible&lt;/span&gt;
    &lt;span class="c1"&gt;// Or you can use an array of value&lt;/span&gt;
    &lt;span class="na"&gt;threshold&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;1.0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;// The callback will run when target element is 50% and 100% visible&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelectorAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;p&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>JS Coding Challenge: Find Anagrams</title>
      <dc:creator>Shafiqul Islam Shuvo</dc:creator>
      <pubDate>Tue, 15 Sep 2020 14:04:27 +0000</pubDate>
      <link>https://dev.to/meghsohor/js-coding-challenge-find-anagrams-4nde</link>
      <guid>https://dev.to/meghsohor/js-coding-challenge-find-anagrams-4nde</guid>
      <description>&lt;h2&gt;
  
  
  What is an anagram?
&lt;/h2&gt;

&lt;p&gt;From Wikipedia:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;An &lt;strong&gt;&lt;em&gt;anagram&lt;/em&gt;&lt;/strong&gt; is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Challenge
&lt;/h2&gt;

&lt;p&gt;Given an array of words, we need to write a function which will take two parameters. First parameter is a word and the second parameter is the array of the words. The function will return an array consisting of the anagrams of the word passed as the first parameter from the array of words passed as the second parameter.&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const words = ['mountain', 'anatomy', 'anemic', 'boldness', 'cinema', 
'iceman', 'machine', 'mechanic', 'elbow', 'below', 'state', 'taste', 
'dusty', 'night', 'study', 'thing', 'search', 'arches', 'chaser', 
'animal', 'manila', 'icewoman'];

const findAnagrams = (word, allWords) =&amp;gt; {
    // Write the code here
};

console.log(findAnagrams('cinema', words));

/* 
    Expected output: ['anemic', 'iceman'];
*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
  
  
  Notes:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;All the words in the returned result should have the same length as the given word.
&lt;strong&gt;Example:&lt;/strong&gt; &lt;code&gt;iceman&lt;/code&gt; and &lt;code&gt;icewoman&lt;/code&gt; are not anagrams. Even though &lt;code&gt;iceman&lt;/code&gt; has every letter as in &lt;code&gt;icewoman&lt;/code&gt; but &lt;code&gt;icewoman&lt;/code&gt; has extra letters in it which &lt;code&gt;iceman&lt;/code&gt; doesn't have.&lt;/li&gt;
&lt;li&gt;The word passed as first parameter should not be included in the returned array. As in the code above you can see that &lt;code&gt;cinema&lt;/code&gt; is not included in the expected output.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Algorithm
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;First, we need to find the total count of each letter in the word. &lt;strong&gt;Example:&lt;/strong&gt; in &lt;code&gt;cinema&lt;/code&gt; each letter has a total count of 1&lt;/li&gt;
&lt;li&gt;Then, we need to loop through each word in the array of words and follow the &lt;strong&gt;Step 1&lt;/strong&gt; for each.&lt;/li&gt;
&lt;li&gt;Then, we need compare the count of each letter between the given word and the current word in the iteration.&lt;/li&gt;
&lt;li&gt;If the current word matches with the given word in terms of the letter and letter counts, we will push that word in the result array.&lt;/li&gt;
&lt;li&gt;Follow &lt;strong&gt;Step 2&lt;/strong&gt; to &lt;strong&gt;Step 4&lt;/strong&gt; until the end of the words array&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Solution
&lt;/h3&gt;

&lt;p&gt;First, we will write a helper function which takes a word converted to an array of letters and will give back an object consisting of each letter in the word as the keys and the total counts of each letter as the value:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numberOfEachLetter = (letters) =&amp;gt; {
    return letters.reduce((acc, letter) =&amp;gt; ({
        ...acc,
        [letter]: acc[letter] ? acc[letter] + 1 : 1,
    }), {});
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the above function we are using &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce"&gt;&lt;strong&gt;Array.reduce()&lt;/strong&gt;&lt;/a&gt; function to create an object of the letters and the count of each letter as the value. We are initiating the &lt;code&gt;.reduce()&lt;/code&gt; function with an empty object &lt;code&gt;{}&lt;/code&gt; which is provided as the second argument of the function. And, in each iteration we are using the &lt;strong&gt;ES6 spread operator&lt;/strong&gt; to get the previous value from and set updated value to &lt;code&gt;accumulator&lt;/code&gt;. And then, using a &lt;strong&gt;ternary&lt;/strong&gt; operator, we are checking if the current letter is already in the &lt;code&gt;accumulator&lt;/code&gt; or not. If it is, then we are incrementing the count, otherwise we are setting 1 as the count value.&lt;/p&gt;

&lt;p&gt;We can call the function like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const word = 'cinema';
numberOfEachLetter(word.split(''));
// Output
{
  a: 1,
  c: 1,
  e: 1,
  i: 1,
  m: 1,
  n: 1
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, we will write another function which can compare between two words using the above &lt;code&gt;numberOfEachLetter&lt;/code&gt;  function:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const hasSameLetterCount = (word1, word2) =&amp;gt; {
    const word1Count = numberOfEachLetter(word1.split(''));
    const word2Count = numberOfEachLetter(word2.split(''));

    return word1.length == word2.length &amp;amp;&amp;amp; 
        Object.keys(word1Count)
          .every(letter =&amp;gt; word1Count[letter] === word2Count[letter]);
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Firstly, here we are getting the objects of letter counts for both words using the &lt;code&gt;hasSameLetterCount&lt;/code&gt; function. Then, we are comparing the length of the two words to make sure that they have exact number of letters. And finally, we are, using the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys"&gt;&lt;strong&gt;Object.keys()&lt;/strong&gt;&lt;/a&gt;, iterating through each letter of the first word and comparing to the letters of second word to check if the letters are same and have the same number of occurrence. Using the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every"&gt;&lt;strong&gt;Array.every()&lt;/strong&gt;&lt;/a&gt; function we are checking that every letter and the count of the letters matches. Otherwise, the function will return false.  &lt;/p&gt;

&lt;p&gt;Okay, enough with the helper functions. Let's face the final function now!&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const findAnagrams = (word, allWords) =&amp;gt; {
    const anagrams = allWords.filter(item =&amp;gt; {
        return word !== item &amp;amp;&amp;amp; hasSameLetterCount(word, item);
    });
    return anagrams;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here, using the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter"&gt;&lt;strong&gt;Array.filter()&lt;/strong&gt;&lt;/a&gt; function, we are iterating through each word in the words array and checking if the current word doesn't match with the given word and then sending the both words to the &lt;code&gt;hasSameLetterCount&lt;/code&gt; function to check if they are matched to be anagrams. And finally returning the array of the filtered words that match with the criteria.&lt;/p&gt;

&lt;p&gt;Does the final function look fat? Here is the slim version using the magic of &lt;strong&gt;ES6&lt;/strong&gt;:&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const findAnagrams = (word, allWords) =&amp;gt; allWords&lt;br&gt;
                     .filter(item =&amp;gt; word !== item &amp;amp;&amp;amp; &lt;br&gt;
                         hasSameLetterCount(word, item));&lt;br&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Note:&lt;br&gt;
&lt;/h4&gt;

&lt;p&gt;I know there are ways to improve the code I have written above. I would appreciate if you can suggest a better way to write the code above.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>codingchallenge</category>
      <category>es6</category>
    </item>
    <item>
      <title>2 useful Polling functions in JavaScript</title>
      <dc:creator>Shafiqul Islam Shuvo</dc:creator>
      <pubDate>Thu, 10 Sep 2020 05:20:45 +0000</pubDate>
      <link>https://dev.to/meghsohor/2-useful-polling-functions-in-javascript-2735</link>
      <guid>https://dev.to/meghsohor/2-useful-polling-functions-in-javascript-2735</guid>
      <description>&lt;p&gt;Sometimes we need to wait for something to be happened before we execute a command or call a function or do something else.&lt;br&gt;
By something I mean:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;rendering of a HTML element&lt;/li&gt;
&lt;li&gt;a JavaScript event&lt;/li&gt;
&lt;li&gt;response from API&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;and many other things.&lt;/p&gt;

&lt;p&gt;Let's see how to write a couple of functions to tackle these scenarios:&lt;/p&gt;



&lt;h3&gt;
  
  
  Poling function 1: wait for HTML element
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var waitForElement = function(elem) {
    if (typeof  elem  ==  'string') {
        return  new Promise(function (resolve) {
            var  wfelem  =  function () {
                if (null  !=  document.querySelector(elem)) {
                    resolve(document.querySelector(elem));
                } else {
                    window.requestAnimationFrame(wfelem);
                }
            };
            wfelem();
        });
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;We can use the above poling function when we need to wait for a certain HTML element.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example:
&lt;/h4&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;waitForElement('button#addToCart').then(function(button) {
     button.textContent = 'Buy Now';
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Poling function 2: wait until a function returns true
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;waitUntil = function(callback) {
    if (typeof callback === 'function') {
        return new Promise(function(resolve, reject) {
        var tick = setInterval(function() {
            if (callback() === true) {
                clearInterval(tick);
                return resolve();
            }
        });
    });
    } else {
        console.error(callback + ' should be a function');
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;We can use the above function to wait until one or more conditions meet the criteria before further execution of the code.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example:
&lt;/h4&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;window.waitUntil(function () {
    return "complete" == document.readyState;
}).then(function () {
    console.log("Page loading complete!");
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Swipe function for touch-devices in Vanilla JS</title>
      <dc:creator>Shafiqul Islam Shuvo</dc:creator>
      <pubDate>Mon, 17 Aug 2020 15:20:04 +0000</pubDate>
      <link>https://dev.to/meghsohor/swipe-function-for-touch-devices-in-vanilla-js-584c</link>
      <guid>https://dev.to/meghsohor/swipe-function-for-touch-devices-in-vanilla-js-584c</guid>
      <description>&lt;p&gt;When we need to implement &lt;strong&gt;Swipe&lt;/strong&gt; functionality for touch devices, we usually use plugins. But we can write touch-swipe functionality in &lt;strong&gt;Vanilla JS&lt;/strong&gt; which supports all the browsers without using any plugin.&lt;/p&gt;

&lt;h5&gt;
  
  
  First We will see the whole function and then we will break the function into small pieces and will talk about the pieces
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var  Swipe  = (function () {
    function  Swipe(element) {
        this.xDown  =  null;
        this.yDown  =  null;
        this.element  =  typeof (element) ===  'string'  ?  document.querySelector(element) :  element;
        this.element.addEventListener('touchstart', function (evt) {
            this.xDown  =  evt.touches[0].clientX;
            this.yDown  =  evt.touches[0].clientY;
        }.bind(this), false);
    }

    Swipe.prototype.onLeft  =  function (callback) {
        this.onLeft  =  callback;
        return this;
    };
    Swipe.prototype.onRight  =  function (callback) {
        this.onRight  =  callback;
        return this;
    };
    Swipe.prototype.onUp  =  function (callback) {
        this.onUp  =  callback;
        return this;
    };
    Swipe.prototype.onDown  =  function (callback) {
        this.onDown  =  callback;
        return this;
    };

    Swipe.prototype.handleTouchMove  =  function (evt) {
        if (!this.xDown  ||  !this.yDown) {
            return;
        }
        var  xUp  =  evt.touches[0].clientX;
        var  yUp  =  evt.touches[0].clientY; 
        this.xDiff  = this.xDown  -  xUp;
        this.yDiff  = this.yDown  -  yUp;

        if (Math.abs(this.xDiff) !==  0) {
            if (this.xDiff  &amp;gt;  2) {
                typeof (this.onLeft) ===  "function"  &amp;amp;&amp;amp; this.onLeft();
            } else  if (this.xDiff  &amp;lt;  -2) {
                typeof (this.onRight) ===  "function"  &amp;amp;&amp;amp; this.onRight();
            }
        }

        if (Math.abs(this.yDiff) !==  0) {
            if (this.yDiff  &amp;gt;  2) {
                typeof (this.onUp) ===  "function"  &amp;amp;&amp;amp; this.onUp();
            } else  if (this.yDiff  &amp;lt;  -2) {
                typeof (this.onDown) ===  "function"  &amp;amp;&amp;amp; this.onDown();
            }
        }
        // Reset values.
        this.xDown  =  null;
        this.yDown  =  null;
    };

    Swipe.prototype.run  =  function () {
        this.element.addEventListener('touchmove', function (evt) {
            this.handleTouchMove(evt);
        }.bind(this), false);
    };

    return  Swipe;
}());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Lets first discuss about the inner &lt;code&gt;Swipe&lt;/code&gt; function
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function  Swipe(element) {
    this.xDown  =  null;
    this.yDown  =  null;
    this.element  =  typeof (element) ===  'string'  ?  document.querySelector(element) :  element;
    this.element.addEventListener('touchstart', function (evt) {
        this.xDown  =  evt.touches[0].clientX;
        this.yDown  =  evt.touches[0].clientY;
    }.bind(this), false);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function has the same name as the Root &lt;code&gt;Swipe&lt;/code&gt; function. Because of that, when we will call the root function, the inner &lt;code&gt;Swipe&lt;/code&gt; function will be initiated. In this function we are setting up the &lt;code&gt;touchstart&lt;/code&gt; event for the target element. And we are capturing the &lt;code&gt;clientX&lt;/code&gt; and &lt;code&gt;clientY&lt;/code&gt; values from the &lt;code&gt;touchstart&lt;/code&gt; event and assigning those values to &lt;code&gt;xDown&lt;/code&gt; and &lt;code&gt;yDown&lt;/code&gt; properties.&lt;/p&gt;

&lt;h5&gt;
  
  
  Now we will add the functions for each swipe direction
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Swipe.prototype.onLeft  =  function (callback) {
    this.onLeft  =  callback;
    return this;
};
Swipe.prototype.onRight  =  function (callback) {
    this.onRight  =  callback;
    return this;
};
Swipe.prototype.onUp  =  function (callback) {
    this.onUp  =  callback;
    return this;
};
Swipe.prototype.onDown  =  function (callback) {
    this.onDown  =  callback;
    return this;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Then we will add the function to detect the Swipe direction
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Swipe.prototype.handleTouchMove = function (evt) {
    if (!this.xDown || !this.yDown) { return; } 
    var xUp = evt.touches[0].clientX; 
    var yUp = evt.touches[0].clientY; 
    this.xDiff = this.xDown - xUp; 
    this.yDiff = this.yDown - yUp; 
    //Swipe Left or Right 
    if (Math.abs(this.xDiff) !== 0) {
        if (this.xDiff &amp;gt; 2) {
            typeof (this.onLeft) === "function" &amp;amp;&amp;amp; this.onLeft(); 
        } else if (this.xDiff &amp;lt; -2) {
            typeof (this.onRight) === "function" &amp;amp;&amp;amp; this.onRight(); 
        } 
    }
    if (Math.abs(this.yDiff) !== 0) {
        if (this.yDiff &amp;gt; 2) {
            typeof (this.onUp) === "function" &amp;amp;&amp;amp; this.onUp(); 
        } else if (this.yDiff &amp;lt; -2) {
            typeof (this.onDown) === "function" &amp;amp;&amp;amp; this.onDown(); 
        } 
    }
    this.xDown = null;
    this.yDown = null; 
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this function, we are tracking the swipe distance and the swipe direction. Based on the swipe direction, we are calling the respective &lt;em&gt;swipe-direction&lt;/em&gt; function: &lt;code&gt;onLeft&lt;/code&gt;, &lt;code&gt;onRight&lt;/code&gt;, &lt;code&gt;onUp&lt;/code&gt; and &lt;code&gt;onDown&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; In the &lt;code&gt;if&lt;/code&gt; conditions, we are checking if the distance is &lt;code&gt;&amp;gt; 2&lt;/code&gt; or &lt;code&gt;&amp;lt; -2&lt;/code&gt;, because it might happen that when the user swipes on a horizontal direction, there might also be a slight vertical movement. And for this movement, the vertical swipe functions will be triggered. That is why, we are checking if the swipe distance is &lt;code&gt;&amp;gt; 2&lt;/code&gt; or &lt;code&gt;&amp;lt; -2&lt;/code&gt; for an extra bit of safety.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  The run function
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Swipe.prototype.run  =  function () {
    this.element.addEventListener('touchmove', function (evt) {
        this.handleTouchMove(evt);
    }.bind(this), false);
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we are adding an event-listener for &lt;code&gt;touchmove&lt;/code&gt; event for the target element.  The &lt;code&gt;handleTouchMove&lt;/code&gt; function will be called once the &lt;code&gt;touchmove&lt;/code&gt; event fires.&lt;/p&gt;

&lt;h3&gt;
  
  
  How can we use this?
&lt;/h3&gt;

&lt;p&gt;First we will create a new object of the function and provide the target element as the parameter&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var swiper = new Swipe('your-target-element');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we will call any of the swipe directional functions (&lt;code&gt;onLeft&lt;/code&gt;, &lt;code&gt;onRight&lt;/code&gt;, &lt;code&gt;onUp&lt;/code&gt; and &lt;code&gt;onDown&lt;/code&gt;) as per our need and inside the callback function we will write our code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;swiper.onLeft(function() {
    //Your code goes here
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Finally run the function
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;swiper.run();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note:&lt;/em&gt;&lt;/strong&gt; this function will work only in the touch devices. You can use browser's &lt;em&gt;Developer Tools&lt;/em&gt; to switch device that supports touch events and can check the function in there.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Final Note:&lt;/em&gt;&lt;/strong&gt; This is my first article, so there might be many mistakes. Also, I believe that this function can be improved in may ways. Any comment or suggestion is much appreciated. &lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>javascript</category>
    </item>
  </channel>
</rss>
