简体   繁体   English

虽然鼠标按下 JS

[英]While mousedown JS

I'm trying to run a function while mousedown but for the life of me I can't get it to work while holding down but everything works by just clicking.我正在尝试在mousedown运行一个函数,但在我的一生中,我无法在按住mousedown运行它,但只需单击即可完成所有工作。 I'm trying to change color of countries on a map as I hold down.当我按住时,我正在尝试更改地图上国家/地区的颜色。 Here's my code:这是我的代码:

    var int;
    var mouseStillDown = false;

      function mousedown(geography) 
      {     console.log('mousedown '+mousedownID);
             mouseStillDown = true;
          int = setInterval( performWhileMouseDown(geography), 100);
      }



    function mouseup() 
    {
        clearInterval(int);  
        mouseStillDown = false;
    }

     function mouseout() 
    {
        clearInterval(int);  
    }

      function performWhileMouseDown(geography)
       {
             if (!mouseStillDown)
              {console.log('error');}

            if (mouseStillDown) {
           if(data[geography.id])
            {
              data[geography.id] ++;
            }else
              {
                data[geography.id] = 1;
              }
            var m = {};                                        
            m[geography.id] = color(data[geography.id]);
            map.updateChoropleth(m);
              }

            /* if (mouseStillDown)
              { setInterval(performWhileMouseDown(geography), 100); }*/
      }

You could try to use mousemove instead, mousedown will only fire once.您可以尝试改用mousemovemousedown只会触发一次。

var mouseDown = false;
window.addEventListener('mousedown', function() { mouseDown = true })
window.addEventListener('mouseup', function() { mouseDown = false })
window.addEventListener('mousemove', function() { 
  if (!mouseDown) {
    return;
  }
  // perform while mouse is moving
})

here's what worked for me这对我有用

 var timeout ;
function mouseDown(geography){

    timeout = setInterval( function(){


            if(data[geography.id]){

              data[geography.id] ++;
            }else{
              data[geography.id] = 1;
            }
            var m = {};                                        
            m[geography.id] = color(data[geography.id]);
            map.updateChoropleth(m);}, 100);

    return false;
}


function mouseUp(geography){

clearInterval(timeout);
    return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM