如何使用javascript计时来控制鼠标停止和鼠标移动事件 [英] How can I use javascript timing to control on mouse stop and on mouse move events

查看:925
本文介绍了如何使用javascript计时来控制鼠标停止和鼠标移动事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在aspx页面上有一个控件(一张地图)。我想写一些javascript来onload设置如下:

So I have a control (a map) on an aspx page. I want to write some javascript to onload setup the following:


  1. 当鼠标停在控制上时=某些代码

  1. when mouse stops on control = some code

当鼠标移动=某些代码时(但仅当移动时间超过250毫秒时)

when mouse moves = some code (but only if the move is longer than 250 mil sec)

这适用于在停止然后移动时触发代码......

This works to trigger code on stop and then on move...

function setupmousemovement() {
var map1 = document.getElementById('Map_Panel');
var map = document.getElementById('Map1');
map1.onmousemove = (function() {
    var onmousestop = function() {
            //code to do on stop
    }, thread;

    return function() {
        //code to do on mouse move
        clearTimeout(thread);
        thread = setTimeout(onmousestop, 25);
    };
    })();
};

但我无法弄清楚如何在移动代码中引入延迟。我以为我有这个... ...

But I cannot figure out how to introduce a delay into the on move code. I thought I had it with this...

function setupmousemovement() {
var map1 = document.getElementById('Map_Panel');
var map = document.getElementById('Map1');
map1.onmousemove = (function() {
    var onmousestop = function() {
            //code to do on stop
            clearTimeout(thread2);
    }, thread;

    return function() {
        thread2 = setTimeout("code to do on mouse move", 250);
        clearTimeout(thread);
        thread = setTimeout(onmousestop, 25);
    };
    })();
};

但它的行为并不像我想象的那样。移动thread2永远不会被停止清除。我错过了什么?

But it does not behave as I thought it would. The on move "thread2" is never cleared by the stop. What am I missing?

推荐答案

这是一个棘手的问题。一点点的修补就是这样:

That is a tricky one. A little bit of tinkering resulted in this:

function setupmousemovement() {

  var map1 = document.getElementById('Map_Panel');
  map1.onmousemove = (function() {
    var timer,
        timer250,
        onmousestop = function() {

          // code to do on stop

          clearTimeout( timer250 ); // I'm assuming we don't want this to happen if mouse stopped
          timer = null;  // this needs to be falsy next mousemove start
        };
    return function() {
      if (!timer) {

        // code to do on start

        timer250 = setTimeout(function () { // you can replace this with whatever

          // code to do when 250 millis have passed

        }, 250 );
      }
      // we are still moving, or this is our first time here...
      clearTimeout( timer );  // remove active end timer
      timer = setTimeout( onmousestop, 25 );  // delay the stopping action another 25 millis
    };

  })();

};

您的代码不起作用的原因是当鼠标移动并且您正在启动时,mousemove会反复触发每次都有新的超时。

The reason your code does not work is that mousemove fires repeatedly while the mouse is moving and you are starting new timeouts every time.

这篇关于如何使用javascript计时来控制鼠标停止和鼠标移动事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆