Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript setTimeout inside setInterval
    primarykey
    data
    text
    <p>I have a problem here.. I use a <code>setInterval</code> function to load a page into a div, once every 30 seconds. Well.. in that page I have another <code>setInterval</code> function, which consists of a countdown that change a number every second. The problem is that at a time, the countdown jumps over 3 seconds, even if it must callback in one second. It's like : 01:24 -> 01:21 -> 01->19...</p> <p>Here is the code for my first setinterval:</p> <pre><code>&lt;script&gt; var t; t=setInterval(function(){ $.get('&lt;?=return_url("php/home_new.php");?&gt;', { k: "1" } ).done(function(data){ if(data)$("#ex_start_test").html(data);}); }, 30000 ); &lt;/script&gt; </code></pre> <p>and the code for countdown:</p> <pre><code>&lt;? $query=q_return("SELECT nume, cod, data, MINUTE(data_start) as minute, data_start FROM chestionare WHERE data_start&gt;date_sub(now(), interval 2 minute)");?&gt; &lt;script&gt; var timp_c="&lt;?=$query['minute'];?&gt;"; var sec; var min; $.get('&lt;?=return_url("php/getimp.php");?&gt;', { k: "sec" } ).done(function(data){sec=data;}); $.get('&lt;?=return_url("php/getimp.php");?&gt;', { k: "min" } ).done(function(data){min=data;}); setTimeout(refresh, 1000); function refresh(){ if(sec&gt;=1){ sec=sec-1; }else{ min=min-1; sec=59; } if(sec&lt;10){ $("span#ex_start_timp_ramas_sec").each(function(){ $(this).html("0"+sec); }); }else{ $("span#ex_start_timp_ramas_sec").each(function(){ $(this).html(sec); }); } $("span#ex_start_timp_ramas_min").each(function(){ $(this).html(min); }); if(sec==0 &amp;&amp; min==0){ //$("#ex_click_final").trigger('click'); clearInterval(t); $("#ex_new_test").hide(); } setTimeout(refresh, 1000); } &lt;/script&gt; </code></pre>
    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.
    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