Javascript倒計(jì)時(shí)頁面跳轉(zhuǎn)實(shí)例小結(jié)
更新時(shí)間:2013年09月11日 11:36:00 作者:
在js中實(shí)現(xiàn)頁面定時(shí)跳轉(zhuǎn)我們要使用setInterval或setTimeOut函數(shù),當(dāng)然還可以使用頁面的meta實(shí)現(xiàn)了,下面使用js實(shí)現(xiàn)的幾個(gè)代碼
例1:
復(fù)制代碼 代碼如下:
<script type="text/javascript" language="JavaScript">
var startTime = new Date();
var endTime=startTime.getTime()+10*60*1000;
var g_blinkswitch = 0;
var g_blinktitle = document.title;
function getRemainTime(){
var nowTime = new Date();
var nMS =endTime - nowTime.getTime();
var nM=Math.floor(nMS/(1000*60)) % 60;
var nS=Math.floor(nMS/1000) % 60;
if(nM==0&&nS==0&&nMS<1000) //當(dāng)?shù)褂?jì)時(shí)結(jié)束
{
window.focus();
setInterval("blinkNewMsg()", 1000);
window.location.reload();
}
if(nS < 10) nS = "0" + nS;
if(nMS >= 0){
document.getElementById("remainTime").innerHTML= nM + "分" + nS + "秒";
setTimeout("getRemainTime()",1000);
}
}
function blinkNewMsg()
{
document.title = g_blinkswitch % 2==0 ? "【 】 - " + g_blinktitle : "【新消息】 - " +
g_blinktitle;
g_blinkswitch++;
}
window.onload=getRemainTime;
</script>
<strong id="remainTime">10分00秒</strong>
例2
倒計(jì)時(shí)跳轉(zhuǎn)頁面
復(fù)制代碼 代碼如下:
<title>JS倒計(jì)時(shí)網(wǎng)頁自動(dòng)跳轉(zhuǎn)代碼</title>
<script language="JavaScript" type="text/javascript">
function delayURL(url) {
var delay = document.getElementById("time").innerHTML;
if(delay > 0) {
delay--;
document.getElementById("time").innerHTML = delay;
} else {
window.top.location.href = url;
}
setTimeout("delayURL('" + url + "')", 1000);
}
</script>
<span id="time" style="background: #00BFFF">3</span>秒鐘后自動(dòng)跳轉(zhuǎn),如果不跳轉(zhuǎn),請點(diǎn)擊下面的鏈接<a href="http://www.dbjr.com.cn">我的百度</a>
<script type="text/javascript">
delayURL("http://www.dbjr.com.cn");
</script>
例3 防刷新的倒計(jì)時(shí)代碼
復(fù)制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title> www.dbjr.com.cn</title>
</head>
<body>
<SCRIPT LANGUAGE="JavaScript">
<!--
var maxtime;
if(window.name==''){
maxtime = 1*60;
}else{
maxtime = window.name;
}
function CountDown(){
if(maxtime>=0){
minutes = Math.floor(maxtime/60);
seconds = Math.floor(maxtime%60);
msg = "距離考試結(jié)束還有"+minutes+"分"+seconds+"秒";
document.all["timer"].innerHTML = msg;
if(maxtime == 5*60) alert('注意,還有5分鐘!');
--maxtime;
window.name = maxtime;
}
else{
clearInterval(timer);
alert("考試時(shí)間到,結(jié)束!");
}
}
timer = setInterval("CountDown()",1000);
//-->
</SCRIPT>
<div id="timer" style="color:red"></div>
</body>
</html>
例4:結(jié)合圖片數(shù)字的現(xiàn)實(shí)更漂亮
代碼中有圖片,直接提供一個(gè)演示地址,注意里面的圖片地址
http://demo.jb51.net/js/2013/daojishi/index.htm
您可能感興趣的文章:
- js頁面跳轉(zhuǎn)常用的幾種方式
- jsp下頁面跳轉(zhuǎn)的幾種方法小結(jié)
- js 控制頁面跳轉(zhuǎn)的5種方法
- js 有框架頁面跳轉(zhuǎn)(target)三種情況下的應(yīng)用
- js頁面跳轉(zhuǎn)的問題(跳轉(zhuǎn)到父頁面、最外層頁面、本頁面)
- jQuery Mobile頁面跳轉(zhuǎn)后未加載外部JS原因分析及解決
- 實(shí)現(xiàn)網(wǎng)頁頁面跳轉(zhuǎn)的幾種方法(meta標(biāo)簽、js實(shí)現(xiàn)、php實(shí)現(xiàn))
- js頁面跳轉(zhuǎn)的常用方法整理
- js實(shí)現(xiàn)頁面跳轉(zhuǎn)的五種方法推薦
- javascript/jquery獲取地址欄url參數(shù)的方法
- JavaScript Base64編碼和解碼,實(shí)現(xiàn)URL參數(shù)傳遞。
- JS實(shí)現(xiàn)頁面跳轉(zhuǎn)參數(shù)不丟失的方法
相關(guān)文章
JavaScript 計(jì)算當(dāng)天是本年本月的第幾周
用來計(jì)算當(dāng)天是本年本月的第幾周的函數(shù)。2009-03-03javascript 寶寶出生倒計(jì)時(shí)實(shí)現(xiàn)代碼
用來計(jì)算寶寶出生的時(shí)間代碼,閑著無聊所作,喜歡的朋友可以參考下。2010-08-08javascript 當(dāng)前日期加(天、周、月、年)
javascript當(dāng)前日期加(天、周、月、年) ,有需要操作日期的朋友,可以參考下。2009-08-08一個(gè)簡單的JavaScript 日期計(jì)算算法
這個(gè)一個(gè)簡單的JavaScript日期計(jì)算算法代碼2009-09-09