[英]countdown timer with JavaScript not work in smarty
Hi guys i use Smarty() and JavaScript to make a table with countdown timer its seems everything is OK but i don't know why i just have one row result for my countdown timer if possible check out my codes and tell me what can i do thank you for all programmers. 嗨,大家好,我使用Smarty()和JavaScript制作一个带有倒数计时器的表格,看来一切正常,但我不知道为什么我的倒数计时器只有一行结果,如果可能的话,请检查我的代码并告诉我该怎么办谢谢所有程序员。 have nice day.
祝你有美好的一天。
HTML: HTML:
<table class="table table-striped table-hover" id="datatable3" cellspacing="0" width="100%">
<thead>
<tr>
<th></th>
<th>Avatar</th>
<th>Player Name</th>
<th>Game</th>
<th>SS Shots</th>
<th>BanTime Remaining</th>
<th>Ban Reason</th>
</tr>
</thead>
<tbody class="tb1">
{foreach $bans as $ban}
<tr class="tr1">
<td class="count"></td>
<td>{$ban.avatar}</td>
<td>{$ban.name}</td>
<td>{$ban.game}</td>
<td>{$ban.ss}</td>
<td>{include file="time.tpl"}</td>
<td>{$ban.reason}</td>
</tr>
{/foreach}
</tbody>
</table>
time.tpl : time.tpl:
<script type="text/javascript" language="javascript">
var now = new Date();
var event = new Date("{$ban.bantime}"*1000);
var milleseconds = (event - now) / 10;
var seconds = milleseconds / 100;
var minutes = seconds / 60;
var hours = minutes / 60;
var days = hours / 24;
var month = days / 30;
var years = days / 365.25;
ID=window.setTimeout("update();", 1);
function update() {
now = new Date();
seconds = (event - now) / 1000;
seconds = Math.floor(seconds);
minutes = seconds / 60;
minutes = Math.floor(minutes);
hours = minutes / 60;
hours = Math.floor(hours);
days = hours / 24;
days = Math.floor(days);
month = month / 30;
month = Math.floor(month);
years = days / 365.25;
years = Math.floor(years);
seconds = seconds - minutes*60;
if (seconds < 10) { seconds = "0" + seconds.toString(); }
minutes = minutes - hours*60;
if (minutes < 10) { minutes = "0" + minutes.toString(); }
hours = hours - days*24;
days = days - Math.floor(years*365.25);
document.getElementById("years").innerHTML = years;
document.getElementById("month").innerHTML = month;
document.getElementById("days").innerHTML = days;
document.getElementById("hours").innerHTML = hours;
document.getElementById("minutes").innerHTML = minutes;
document.getElementById("seconds").innerHTML = seconds;
ID=window.setTimeout("update();",500);
}
</script>
<table cellspacing="0" cellpadding="0" border="0" >
<tbody class="{$ban.userID}">
<tr>
<td><span id="years"></span></td>
<td><span id="month"></span></td>
<td><span id="days"></span></td>
<td><span id="hours"></span></td>
<td><span id="minutes"></span></td>
<td><span id="seconds"></span></td>
</tr>
<tr>
<td><span>years</span></td>
<td><span>month</span></td>
<td><span>days</span></td>
<td><span>hours</span></td>
<td><span>min</span></td>
<td><span>sec</span></td>
</tr>
</tbody>
</table>
But I have just one row result for my code, why? 但是我的代码只有一行结果,为什么呢? See this screenshot .
查看此屏幕截图 。
Your problem is that setTimeout
takes a function pointer as an argument, not a string with the function name. 您的问题是
setTimeout
将函数指针作为参数,而不是带有函数名称的字符串。
Thus, try this: ID = setTimeout(update, 500);
因此,请尝试以下操作:
ID = setTimeout(update, 500);
For more information, check out the docs . 有关更多信息,请查看docs 。 They also have an example.
他们也有一个例子。
document.getElementById("years")
returns an element whose id is yearsdocument.getElementById("years")
返回其id为years 的元素
And in your case I can see only one element whose id is years . 在您的情况下,我只能看到id为years的一个元素。
If you want to change a group of elements, you might want give them a class name and then perform your operation on all the elements. 如果要更改一组元素,则可能要给它们一个类名,然后对所有元素执行操作。
For example: 例如:
// If you keep *multiple rows* something like this (not sure about your table structure)
<tbody class="{$ban.userID}">
<tr>
<td><span class="years"></span></td>
...
</tr>
<tr>
<td><span>years</span></td>
...
</tr>
<tr>
<td><span class="years"></span></td>
...
</tr>
<tr>
<td><span>years</span></td>
...
</tr>
</tbody>
</table>
/** This would do the trick **/
document.getElementsByClassName("years").innerHTML = years;
// ...
` `
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.