zoukankan      html  css  js  c++  java
  • 定时刷新和跳转和停止

    在servlet中

    来看一个曾经的项目,跳转到这个Servlet的dopost

    @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            try {
                testPay();
                resp.getWriter().write("ok,查看控制台");
    //            Thread.sleep(3000);停三秒
    //            resp.sendRedirect("http://localhost:8080/");
                //设置3秒钟跳转
                resp.setHeader("refresh", "3;url=http://localhost:8080/");
            } catch (Exception e) {
                e.printStackTrace();
                resp.getWriter().write("error");
                resp.setHeader("refresh", "3;url=http://localhost:8080/");
            }

    这里面的问题是如果当初行的是用线程停止,那么什么也不会做,也不会跳转去打印ok,只有用setHeader才行

    或者req

    //第二种方式,设置request的属性定时跳转
        private void test2(HttpServletRequest request,HttpServletResponse response){
            String meta="<meta http-equiv='Refresh' content='3;url=/web2/show.jsp'>";
            request.setAttribute("meta", meta);
            try {
                request.getRequestDispatcher("/main.jsp").forward(request, response);
            } catch (ServletException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    在html中

    最简单的一种:直接在前面<head>里面添加代码: 

    <span style="font-size:18px;"> </span>
    <span style="font-size:24px;">
    <meta http-equiv="refresh" content="3;URL=res.html"> 
    </span> 
    <span style="font-size:24px;">//3秒之后自动跳转到res.html,两个属于同一文件下面,要是需要跳转到jsp页面,
    就需要在url里面填写url地址————(浏览器的地址栏里面写入的数据,如:
    http://localhost:8080/TestDemo/1.jsp)</span>

    setTimeout 经过指定毫秒值后计算一个表达式

    具体实现如下:

    复制代码
    <script type="text/javascript"> 
    onload=function(){ <span style="white-space:pre"> </span>//在进入网页的时候加载该方法 
    setTimeout(go, 3000); <span style="white-space:pre"> </span> /*在js中是ms的单位*/ 
    }; 
    function go(){ 
    location.href="http://localhost:8080/TestDemo/index.jsp"; 
    } 
    </script> 
    //3秒之后自动执行go方法,直接跳转到index.jsp页面 
    复制代码


    上面两个例子的缺陷就是能够实现跳转,但是不知道是什么时候跳转.实现倒数3-2-1; 

    settimeout方法已经做不了了; 

    setInterval 每经过指定毫秒值后计算一个表达式。 

    没过相同的时间,就会执行相应的函数。具体的实现方法: 

    复制代码
    <script type="text/javascript"> 
    onload=function(){ 
    setInterval(go, 1000); 
    }; 
    var x=3; //利用了全局变量来执行 
    function go(){ 
    x--; 
    if(x>0){ 
    document.getElementById("sp").innerHTML=x; //每次设置的x的值都不一样了。 
    }else{ 
    location.href='res.html'; 
    } 
    } 
    </script> 
    复制代码
  • 相关阅读:
    礼物的最大价值
    复杂链表的复制
    全排列(回溯法)
    删除排序数组中的重复项
    三角形最小路径和(经典dp)
    链表中倒数第k个节点
    造成segment fault,产生core dump的可能原因
    wmpnetwk.exe怎么禁启动
    GSM/GPRS/EDGE/WCDMA/HSDPA/HSUPA--辨析
    OSI七层参考模型每一层都有哪些协议
  • 原文地址:https://www.cnblogs.com/yangj-Blog/p/13129909.html
Copyright © 2011-2022 走看看