Increase a series each time a function is called
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?
This is the .php file:
<body>
<script type="text/javascript">
foreach($objects as $key => $ar1)
{
countdown_clock_abs(<?php echo $ar1['timespamSale']; ?>,1);
}
</script>
</body>
</html>
This is the JavaScript:
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 = '<div id="countdown<?php echo ar1['id']; ?>"></div>';
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 < 0)
Time_Left = 0;
var innerHTML = '';
switch(format)
{
case 0:
开发者_JAVA百科 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<?php echo ar1['id']; ?>').innerHTML = innerHTML;
setTimeout('countdown_abs(' + time + ',' + format + ');', 50);
}
Modify the signature of your method to match function countdown_clock_abs(time, format, id)
. Now in your first block of code, you can pass the counter to the id param
精彩评论