Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript countdown timer that stops when window is not in focus
    primarykey
    data
    text
    <p>Ok , I'm having trouble to solve this , I'm a php / C# web developer , and have no experience or knowledge in Javascript, I have to do just this one thing that needs Javascript:</p> <p>When a certain page loads, a counter starts. The client must stay on this page for 20 seconds. after, I want to execute php code.</p> <p>So there are 2 issues concerning me, first: how do I stop the counter, if client leaves the page (meaning the page is not in focus).</p> <p>2) How can I execute php in javascript? , or call a php function from Javascript.</p> <p>The code I have so far is this:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;/head&gt; &lt;body&gt; &lt;div id='timer'&gt; &lt;script type="text/javascript"&gt; COUNTER_START = 20 function tick () { if (document.getElementById ('counter').firstChild.data &gt; 0) { document.getElementById ('counter').firstChild.data = document.getElementById ('counter').firstChild.data - 1 setTimeout ('tick()', 1000) } else { document.getElementById ('counter').firstChild.data = 'done' } } if (document.getElementById) onload = function () { var t = document.createTextNode (COUNTER_START) var p = document.createElement ('P') p.appendChild (t) p.setAttribute ('id', 'counter') var body = document.getElementsByTagName ('BODY')[0] var firstChild = body.getElementsByTagName ('*')[0] body.insertBefore (p, firstChild) tick() } &lt;/script&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>and I also want the timer to start ticking when the client gets back on page</p> <p>Thank you very much for ur help in advance</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload