一、效果
![在这里插入图片描述](https://img-blog.csdnimg.cn/e6ba8528c03b4818a9c891d1e05be502.png#pic_left)
二、代码
public function test() {
self::jumpPage('http://www.baidu.com');
}
public function jumpPage($jumpUrl, $message = '操作成功', $time = 5) {
$html = <<<EOF
<p>{$message}</p>
<p>
系统将在
<span style="color:blue;">
<strong>{$time}</strong>
</span>
秒后自动跳转,或直接点击
<a href="{$jumpUrl}">
这里
</a>
跳转
</p>
<script>
let number = document.getElementsByTagName("strong")[0].innerHTML; //获取元素的秒数
//根据元素的秒数,根据定时器减少秒数
function countDown() {
number --; //秒数递减
document.getElementsByTagName("strong")[0].innerHTML = number; //设置元素的秒数-1
if (number <= 0) { //如果秒数 <= 0时,页面递减
// window.open('{$jumpUrl}'); //新窗口打开链接
window.location.href = '{$jumpUrl}'; //在原来的窗口打开链接
}
}
setInterval("countDown()", 1000); //每隔1s执行1次
</script>
EOF;
echo $html;
}