一、效果

在这里插入图片描述

二、代码

    public function test() {
        self::jumpPage('http://www.baidu.com');
    }

    /**
     * 页面跳转公共方法
     * @param string $jumpUrl 需要跳转的url
     * @param string $message 提示信息
     * @param int $time 多少秒之后跳转
     */
    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;
    }