JS定时器实现页面N秒后跳转 实现每隔 1s 自动刷新页面并格式化的显示当前时间 您所在的位置:网站首页 网页设置刷新时间 JS定时器实现页面N秒后跳转 实现每隔 1s 自动刷新页面并格式化的显示当前时间

JS定时器实现页面N秒后跳转 实现每隔 1s 自动刷新页面并格式化的显示当前时间

2024-07-06 07:20| 来源: 网络整理| 查看: 265

1. 通过 setInterval 函数,来周期性的更新倒计时间,同时更新到页面。即通过设置页面可以显示 3 2 1,然后跳转。1000指的是每隔1秒执行一次。

Title let timer = 3; // 设置跳转时间 let div = document.querySelector('.one'); setInterval(function() { if(timer==0) { location.href = "http://www.baidu.com"; //跳转目的地 } else { div.innerHTML = "您将在"+timer+"秒后跳转到百度"; timer--; } },1000); // 1000毫秒 一秒执行一次

2. 通过 setTimeout 执行一个延迟函数来达到跳转的目的。

Title 将在3秒后跳转到百度页面 setTimeout(function() { window.location.href = "http://www.baidu.com"; },3000); // 3000毫秒 在这设置跳转时间

 

3. SimpleDateFormat

//第一种 @WebServlet("/aaa") protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf8"); resp.setContentType("text/html;charset=utf8"); // 设置刷新时间为一秒 resp.setHeader("Refresh","1"); // 格式化日期 DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //这种是24小时制 //DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); //这种是12小时制 String format1 = format.format(new Date()); resp.getWriter().write("当前时间 "+format1); } //第二种 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html; charset=utf-8"); // 设置Refresh时间为1秒 resp.setHeader("Refresh", "1"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 24小时制 String time = sdf.format(new Date()); resp.getWriter().write("当前时间:" + time); }

 或者

@WebServlet("/aaa") public class test extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html; charset=utf-8"); // 设置Refresh时间为1秒 resp.setHeader("Refresh", "1"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = sdf.format(new Date()); resp.getWriter().write("当前时间:" + time); } }



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有