title: How to determine in js whether an app is installed on a phone?
body: <p>The question concerns js more than the specific task of Telegram determination.</p>
  
  <p>In the normal case, on a web page there is code that, in case when an app is installed (in our case it's Telegram), sends the user to the app at once.</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      var url = "tg://resolve?domain=";
          setTimeout(function() {
              window.location = url;
          }, 100);
      &lt;/script&gt;
  </code></pre>
  
  <p>There is also code that, for example, will also open a page in a browser after some time.</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      var now = new Date().valueOf();
      setTimeout(function () {
          if (new Date().valueOf() - now &gt; 200) return;
          window.location = "https://itunes.apple.com/";
      }, 25);
      window.location = "tg://resolve?domain=";
  &lt;/script&gt;
  </code></pre>
  
  <p>Meanwhile, we would like only one of the options to work - either an app or a web page should open (if the app isn't installed).</p>
  
  <p>In this regard, there is a question if we can somehow monitor the following developments:
  - the user left the page but didn't open it (some events that can prompt that the user doesn't interact with the page). We mean that in this case the user opened Telegram and it is installed.
  - if it's possible, to note the time during which the user didn't interact with the page (in case when the user went to Telegram and after some time - e. g., a few minutes - came back to the browser on our page).</p>
  
  <p>So, the algorithm is like this:
  if Telegram is installed, then we suppose the tab with our web page will be open, and no actions will be performed on the page - for quite long, at least 20 seconds.
  if Telegram is not installed, then the user will either close the web page or will somehow interact with it (scroll, click, etc.).</p>