js实现几秒倒计时之后自动跳转页面
程序员文章站
2022-05-13 23:11:59
...
点击按钮之后 自动跳转到百度
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button>跳转百度</button>
<div></div>
<script>
var btn = document.querySelector('button');
var div = document.querySelector('div')
btn.addEventListener('click', function() {
var times = 5;
setInterval(function() {
if (times == 0) {
location.href = 'http://www.baidu.com';
times = 5;
} else {
div.innerHTML = '还剩' + times + '秒自动跳转'
times--;
}
}, 1000)
})
// 五秒后直接跳转
var times = 5
setInterval(function() {
if (times == 0) {
location.href = 'http://www.baidu.com';
times = 5;
} else {
div.innerHTML = '还剩' + times + '秒自动跳转'
times--;
}
}, 1000)
</script>
</body>
</html>
下一篇: GC机制