前天,老板提了一个需求,提供给用户的下载说明文档,当用户打开之后,等待5秒后自动跳转到公司的官网上,那这个是怎么实现?
http://mpvideo.qpic.cn/0bc3geac4aaaziablwrtczrfamodfyyqalqa.f10002.mp4?dis_k=4df19aac90d1f16d959bd4075f5af0d6&dis_t=1648724264&vid=wxv_2314124829832445954&format_id=10002&support_redirect=0&mmversion=false
方法1-使用定时器实现
setTimeout(()=> {
window.location.href = "https://itclan.cn";
},5000)()
// 等价于
var timer = setTimeout(() => {
window.location.href = "https://itclan.cn";
// 等价于
window.open("https://itclan.cn","_self"); // 当前窗口打开
},5000);
timer(); // 此时需要调用一下
以上这种方法是大家比较常用的,也是第一时间能想到的,今天要介绍的是第二种方法,使用一行代码即可解决
方法2-使用http-equiv="refresh”
<meta http-equiv="refresh" content="5;url=https://www.itclan.cn/">
在页面的头部插入meta标签,并且使用http-equiv="refresh",并结合content属性,第一个参数为多长时间,第二个参数url为跳转的指定的网址
第二种方法是比较巧妙的,而且也不用写js代码,逻辑,推荐小白使用
你学会了?