支付10秒倒計時
好久沒有寫CSDN了,從今天起慢慢搞起來,菜鳥一枚,請多多指教!下面直接上代碼。
index.html頁面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>支付10秒倒計時</title>
</head>
<style>
div{
background: gray;
width: 200px;
height: 280px;
padding: 20px;
margin: 0 auto;
}
button{
margin: 30px 25px;
}
</style>
<body>
<div>
<p>商品:web前端課程</p>
<p>原價:1980元</p>
<p>現價:1.98元</p>
<p>内容:HTML、CSS、JS</p>
<p>位址:北京市昌平區</p>
<button>取消</button>
<button>支付</button>
</div>
<script type="text/javascript">
document.getElementsByTagName('button')[1].onclick = function(){
//console.log(1);測試
let res = window.confirm("您确定要支付嗎?");
if(res){
location.href = 'pay.html'
}
}
</script>
</body>
</html>
運作結果:
pay.html頁面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
div{
margin: 0 auto;
width: 500px;
}
#jumpTo{
color: red;
font-size: 30px;
}
</style>
</head>
<body>
<div>
<h2>恭喜你,支付成功</h2>
<span id="jumpTo">10</span>秒後自動傳回首頁
<p><button>立即傳回</button></p>
</div>
<script type="text/javascript">
//打開頁面,應該觸發定時器 10s
window.onload = function(){
//箭頭函數
let timer = 10;
setInterval(()=>{
timer--;
document.getElementById('jumpTo').innerHTML = timer;
if(timer == 0){
location.href = 'index.html';
}
},1000);
}
document.getElementsByTagName('button')[0].onclick = function(){
location.href = 'index.html';
}
</script>
</body>
</html>
運作結果
打完收功