给个JS写的"倒数"类
countdown.js
function countdown() {
this.second = 0;
this.url = "";
this.idinterval = false;
this.isStop = false;
window.obj = this;
this.cd = function() {
this.second--;
if (this.second <= 0) {
window.location = this.url;
clearInterval(this.idinterval);
}
this.writesecond();
}
this.cd_start = function() {
if (document.getElementById("span_countdown") == null)
document.write("<span id=\"span_countdown\" style=\"color:#0066CC\">" + this.second + "</span>");
this.idinterval = setInterval("obj.cd()",1000);
}
this.cd_stop = function() {
clearInterval(this.idinterval);
}
this.writesecond = function() {
document.getElementById("span_countdown").innerHTML = this.second;
}
this.stop_and_goon = function() {
if (this.isStop) {
this.isStop = false;
this.cd_start();
}
else {
this.isStop = true;
this.cd_stop();
}
}
}
使用例子:
<script language="javascript" src="countdown.js"></script>
在
<script language="javascript">
var cd = new countdown();
cd.second = 9;
cd.url = "<?=$_GET['url']?>";
cd.cd_start();
</script>
秒后自动跳转
<p>
<a href="###" onclick="cd.stop_and_goon()">暂停|开始</a>