setTimeout 定时器

1.快速多次点击只触发最后一次

var hander = 0;

    function btnClick() {

        if (hander != 0) {

            clearTimeout(hander);

            hander = 0;

        }

        hander = setTimeout(function () {

            Run();

        }, 300);

    }

    function Run() {

        console.log(‘Run‘);

    }

<input type="button" onclick="btnClick()" value="快速点击只触发最后一次" />

时间: 2024-08-08 21:54:32

setTimeout 定时器的相关文章

javascript中window与document对象、setInterval与setTimeout定时器的用法与区别

一.写在前面 本人前端菜鸟一枚,学习前端不久,学习过程中有很多概念.定义在使用时容易混淆,在此给向我一样刚踏入前端之门的童鞋们归纳一下.今天给大家分享一下js中window与document对象.setInterval与setTimeout定时器的用法与区别.讲得不对的地方,烦请大家指正,还望前辈.大牛多多指教! 二.window对象与document对象的用法和区别 window是全局对象,document是window对象的一个属性它也是一个对象.如图: document对象指的页面这个文档

setInterval和setTimeout定时器

1,文本框自增(重零开始)每隔一秒递增1 <input type="text" name="name" value="0" id="txt" /><script type="text/javascript"> window.onload = function () { setInterval(function () { //获取文本框对象 var obj = document.get

setTimeout 定时器的使用

setTimeout(function(){ //内容 },200);

JS中定时器setTimeout,setInterval,clearTimeout,clearInterval用法

setTimeout是指过多久执行,只执行一次 setInterval是指每过多久执行一次 clearTimeout是关闭setTimeout定时器 clearInterval是关闭setInterval定时器,不让它一直执行 <html> <head> <title></title> </head> <style> </style> <script> window.onload=function(){ var

JavaScript定时器原理分析

.header { cursor: pointer } p { margin: 3px 6px } th { background: lightblue; width: 20% } table { text-align: center; margin-top: 20px; margin-left: 10px; margin-bottom: 20px } a { cursor: pointer; text-decoration: none; color: gray } a:hover { text

关于JavaScript定时器我的一些小理解

因为自己在平时工作中,有些功能需要用到定时器,但是定时器并不像我们表边上看到的那样,所以这周末我看看书查查资料,深入研究了一下JavaScript中的定时器,那么废话不多说,下面进入我们今天的正题. 大家都知道JavaScript是单线程的,所以不管是定时器还是用户的操作都是需要在线程队列中排队执行的. 一.定时器在执行线程队列里的分析 为了更好的理解我还是直接写个测试代码来看一下,这样分析起来更直观一些 1 <script type="text/javascript">

闭包应用之延迟函数setTimeout

根据HTML 5标准,setTimeout推迟执行的时间,最少是5毫秒.如果小于这个值,会被自动增加到5ms. 每一个setTimeout在执行时,会返回一个唯一ID,把该ID保存在一个变量中,并传入clearTimeout,可以清除定时器. 在setTimeout内部,this绑定采用默认绑定规则,也就是说,在非严格模式下,this会指向window:而在严格模式下,this指向undefined. 一.用setTimeout代替setInterval 由于setInterval间歇调用定时器

关于文章《for循环里面设置setTimeout弹出数据顺序是乱的》的一些问题

代码1:for(var i=0;i<10;i++){         (function(index){             setTimeout(function (){                 alert(index);             },1000);         })(i); } 代码2: for(var i=0;i<10;i++){         (function(index){             setTimeout(a(index),1000);

探索setTimeout

其实说起JavaScript中的定时器(Timer)中的 setTimeout() 方法,从事开发的同学想必都不会陌生,觉得这些东西很简单很基础.但是有时候恰恰是基础简单的东西,才越容易被忽略.先看一段代码: console.log("start"); setTimeout(function(){ console.log("world") },200); setTimeout(function(){ console.log("Hello ") }