当前位置: 首页 > article >正文

JS定时器的用法及示例

JS定时器的用法及示例

 

目录

js 定时器的四个方法

示例一

示例二

示例三

 

js 定时器的四个方法

  • setInterval() :按照指定的周期(以毫秒计)来调用函数或计算表达式。方法会不停地调用函数,直到 clearInterval() 被调用或窗口被关闭。
  • setTimeout() :在指定的毫秒数后调用函数或计算表达式。
  • clearTimeout(timer):取消由setTimeout()设置的定时操作。
  • clearInterval(timer):取消由setInterval()设置的定时操作。

setInterval()与clearInterval(timer)

语法

setInterval(code,millisec,lang)
参数描述
code必需。要调用的函数或要执行的代码串。
millisec必须。周期性执行或调用 code 之间的时间间隔,以毫秒计。
lang可选。 JScript | VBScript | JavaScript

以下实例在每 1000 毫秒执行 clock() 函数。实例中也包含了停止执行的按钮:

<html>
<body><input type="text" id="clock" />
<script type="text/javascript">
var int=self.setInterval("clock()",1000);
function clock()
{
var d=new Date();
var t=d.toLocaleTimeString();
document.getElementById("clock").value=t;
}
</script><button οnclick="int=window.clearInterval(int)">停止</button></body>
</html>

setTimeout()与clearTimeout()

语法

setTimeout(code,millisec,lang)
参数描述
code必需。要调用的函数后要执行的 JavaScript 代码串。
millisec必需。在执行代码前需等待的毫秒数。
lang可选。脚本语言可以是:JScript | VBScript | JavaScript

实例演示如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body><p>点击按钮,在等待 3 秒后弹出 "Hello"。</p>
<button οnclick="myFunction()">点我</button><script>
function myFunction()
{setTimeout(function(){alert("Hello")},3000);
}
</script></body>
</html>

 

 

示例一

示例一我们将用定时器做一个鼠标点击定向移动div的demo。

<!DOCTYPE html>
<html><head><meta charset="UTF-8"><title></title><style type="text/css">*{margin:0;padding: 0;}#box{position:absolute;left: 0;height:100px;width:100px;background:#000;}#bt{position: absolute;top:110px;}</style><script type="text/javascript">window.onload=function(){var bt=document.getElementById("bt");var box=document.getElementById("box");var timer;bt.onclick=function(){//关闭上一次定时器
                clearInterval(timer);//开启一个定时器    timer=setInterval(function(){var oldvalue=parseInt(getStyle(box,"left"));var newvalue=oldvalue+10;if(newvalue>800){newvalue=800;}box.style.left=newvalue+"px";//当元素到达800关闭定时器if(newvalue===800)clearTimeout(timer);},30);};};function getStyle(obj,name){if(window.getComputedStyle){return getComputedStyle(obj,null)[name];}else{return obj.currentStyle[name];}};</script></head><body><div id="box"></div><button id="bt">开始</button></body>
</html>

 

 

示例二

示例一我们将用定时器做一个鼠标点击可以左右移动div的demo。

<!DOCTYPE html>
<html><head><meta charset="UTF-8"><title></title><style type="text/css">*{margin:0;padding: 0;}#box{position:absolute;left: 0;height:100px;width:100px;background:#000;}#bt{position: absolute;top:110px;}#bt1{position:absolute;top:110px;left:50px;}</style><script type="text/javascript">window.onload=function(){var bt=document.getElementById("bt");var bt1=document.getElementById("bt1");var box=document.getElementById("box");var timer;bt.onclick=function(){move(box,10,800);};bt1.onclick=function(){move(box,10,0);};//动画函数/*参数* -1.obj 对象* -2.speed 速度* -3.target 执行动画的目标*/function move(obj,speed,target){clearInterval(timer);//判断速度的正负值//如果从0向800移动则为正//如果从800向0移动则为负var current=parseInt(getStyle(obj,"left"));if(current>target){speed=-speed;}//开启一个定时器timer=setInterval(function(){//关闭上一次定时器var oldvalue=parseInt(getStyle(obj,"left"));var newvalue=oldvalue+speed;if(speed<0&&newvalue<target||speed>0&&newvalue>target){newvalue=target;}obj.style.left=newvalue+"px";//当元素到达target关闭定时器if(newvalue===target||newvalue===target)clearTimeout(timer);},30);};};function getStyle(obj,name){if(window.getComputedStyle){return getComputedStyle(obj,null)[name];}else{return obj.currentStyle[name];}};</script></head><body><div id="box"></div><button id="bt">左移</button><button id="bt1">右移</button></body>
</html>

 

 

 

示例三

示例三中我们对move函数做了扩展及封装。

<!DOCTYPE html>
<html><head><meta charset="UTF-8"><title></title><style type="text/css">*{margin:0;padding: 0;}#box{position:absolute;top:30px;left: 0;height:100px;width:100px;background:#000;}#box2{position:absolute;top:200px;left: 0;height:100px;width:100px;background:yellow;}</style><script type="text/javascript" src="js/tools.js"></script><script type="text/javascript">window.onload=function(){var bt=document.getElementById("bt");var bt1=document.getElementById("bt1");var bt2=document.getElementById("bt2");var bt3=document.getElementById("bt3");var box=document.getElementById("box");var box2=document.getElementById("box2");//var timer;bt.οnclick=function(){move(box,10,800,"left");};bt1.onclick=function(){move(box,10,0,"left");};bt2.onclick=function(){move(box2,10,800,"left");};bt3.onclick=function(){move(box2,10,800,"width",function(){move(box2,10,400,"height",function(){move(box2,10,100,"width",function(){move(box2,10,100,"height",function(){});});});});};};</script></head><body><button id="bt">box右移</button><button id="bt1">box左移</button><button id="bt2">box2右移</button>        <button id="bt3">测试</button>        <div id="box"></div><div id="box2"></div></body>
</html>

tool.js

                //动画函数/*参数* -1.obj 对象* -2.speed 速度* -3.target 执行动画的目标* -4.arrt 要变化的样式* -5.callback:回调函数 将会在动画执行完后执行*/function move(obj,speed,target,arrt,callback){//关闭上一次定时器
                        clearTimeout(obj.timer);//判断速度的正负值//如果从0向800移动则为正//如果从800向0移动则为负var current=parseInt(getStyle(obj,arrt));if(current>target){speed=-speed;}//开启一个定时器//为obj添加一个timer属性,用来保存它1自己的定时器的标识obj.timer=setInterval(function(){//获取原来的left值var oldvalue=parseInt(getStyle(obj,arrt));//在旧值的基础上增加var newvalue=oldvalue+speed;if(speed<0&&newvalue<target||speed>0&&newvalue>target){newvalue=target;}obj.style[arrt]=newvalue+"px";//当元素到达target关闭定时器if(newvalue===target||newvalue===target){clearTimeout(obj.timer);//动画执行完 执行函数callback&&callback();}},30);};function getStyle(obj,name){if(window.getComputedStyle){return getComputedStyle(obj,null)[name];}else{return obj.currentStyle[name];}};

 

 

转载于:https://www.cnblogs.com/-wenli/p/11156973.html

http://www.lryc.cn/news/2420923.html

相关文章:

  • AI妻子生成器:科技陪伴,情感无限
  • 解决size mismatch for embedding.embed_dict.userid.weight
  • 单片机——LCD1602
  • 移动测试之-流量测试方案
  • Visual Studio 2008 试用版评估期已结束的解决方法
  • 一步步优化JVM七:其他
  • 无法启动计算机上的服务msdtc,MSDTC服务无法启动解决方法
  • 分享116个ASP搜索链接源码,总有一款适合您
  • Hello C++
  • 纳什均衡定义、举例、分类
  • 开启游戏别样体验:《下一站江湖2》风灵月影六十项修改器使用手册
  • ubuntu9.10 软件推荐
  • Oracle DB Time 解读
  • 收集一些有质感、有内涵的网站 (转载)
  • 实时监控系统介绍
  • MapInfo是一种流行的地理信息系统(GIS)软件,它提供了丰富的功能和工具,用于处理、分析和可视化地理空间数据
  • CAN总线学习笔记 | CAN基础知识介绍
  • 2024年最全在线查询默认密码网站--分享_hawel-lutuo默认密码(1),分析网络安全未来几年的发展前景
  • java计算机毕业设计电商网站在线客服(附源码+springboot+开题+论文+部署)
  • 递归和迭代_深究递归和迭代的区别、优缺点及实例对比
  • 网络层 IPV4报文格式
  • 中国网站广告联盟大集合
  • 5.秒杀模块-基于redis缓存商品秒杀信息
  • ‘真三国无双5’完美存档修改
  • 图像对抗生成网络 GAN学习01:从头搭建最简单的GAN网络,利用神经网络生成手写体数字数据(tensorflow)
  • gitgitlab 修改本地分支名称和远程分支名称
  • 初探Spark-使用大数据分析2000W行数据
  • 博客屋网址导航自适应主题php源码
  • 驱动python_光驱驱动下载_万能光驱驱动(万能DVD光驱CD光驱驱动) 2018 官方版_极速下载站...
  • MFC框架机制详解