js定时器

来源:互联网 发布:绕过公司屏蔽上淘宝网 编辑:程序博客网 时间:2024/06/04 23:21
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'Time.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->


  </head>
   
  <body>
    <div  id="div" style="width:100%;height:100%; background-color: white" align="right" >

</div>
  </body>
   <script type="text/javascript">
  function time(){
  var date=new Date();
  var time=date.toLocaleString();
  var div=document.getElementById("div");
  div.innerHTML=time;//写入页面
 
  }
  setInterval("time()", 1000);
 
  </script>
</html>


以上源码

window.setInterval()

功能:

定时函数,周期以毫秒为单位,

语法: var timerId=setInterval("fn()",3000)

fn() 表示函数,3000表示没3秒执行一次

timerId : 定时器返回的 id 

注意: setInterval()buhui 停止调用函数. 直到执行clearInterval() ,或者窗口关闭.

取消定时器:

使用 clearInterval(id_of_Interval)

id_of_interval 为 setinterval 返回的id




原创粉丝点击