而mousedown JS

时间:2016-12-09 19:48:01

标签: javascript d3.js javascript-events mouseevent onmousedown

我试图在mousedown时运行一个功能,但是对于我的生活,我不能让它在按住的同时工作,但只需单击即可完成所有工作。我按住时试图在地图上更改国家/地区的颜色。 这是我的代码

    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); }*/
      }

2 个答案:

答案 0 :(得分:1)

您可以尝试使用mousemove,而mousedown只会触发一次。

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
})

答案 1 :(得分:0)

这是对我有用的东西

 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;
}