Note that there are some explanatory texts on larger screens.

plurals
  1. POIncrease a series each time a function is called
    primarykey
    data
    text
    <p>I have divs displayed with a PHP script using foreach. Each div should call a JavaScript function and generate a new div unic with id but I don't know how to increment the series. I need a incrementing loop each time the a div calls the function. Is that possible?</p> <p>This is the .php file:</p> <pre><code>&lt;body&gt; &lt;script type="text/javascript"&gt; foreach($objects as $key =&gt; $ar1) { countdown_clock_abs(&lt;?php echo $ar1['timespamSale']; ?&gt;,1); } &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>This is the JavaScript:</p> <pre><code>function countdown_clock_abs(time, format) { // I expected the foreach loop do the job but the php it is loaded before the javascrip is launched. So clearly this is not the way to do it. html_code = '&lt;div id="countdown&lt;?php echo ar1['id']; ?&gt;"&gt;&lt;/div&gt;'; document.write(html_code); countdown_abs(time, format); } function countdown_abs(time, format) { var d=new Date(); var Target_Time = (time); var Now_time = ((d.getTime())); Time_Left = Math.round(Target_Time-(Now_time)/1000); if(Time_Left &lt; 0) Time_Left = 0; var innerHTML = ''; switch(format) { case 0: innerHTML = Time_Left + ' seconds'; break; case 1: days = Math.floor(Time_Left / (60 * 60 * 24)); Time_Left %= (60 * 60 * 24); hours = Math.floor(Time_Left / (60 * 60)); Time_Left %= (60 * 60); minutes = Math.floor(Time_Left / 60); Time_Left %= 60; seconds = Time_Left; dps = 's'; hps = 's'; mps = 's'; sps = 's'; if(days == 1) dps =''; if(hours == 1) hps =''; if(minutes == 1) mps =''; if(seconds == 1) sps =''; innerHTML = days + ' day' + dps + ' '; innerHTML += hours + ' hour' + hps + ' '; innerHTML += minutes + ' minute' + mps + ' and '; innerHTML += seconds + ' second' + sps; break; default: innerHTML = Time_Left + ' seconds'; } document.getElementById('countdown&lt;?php echo ar1['id']; ?&gt;').innerHTML = innerHTML; setTimeout('countdown_abs(' + time + ',' + format + ');', 50); } </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.
    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