簡體   English   中英

雖然鼠標按下 JS

[英]While mousedown JS

我正在嘗試在mousedown運行一個函數,但在我的一生中,我無法在按住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); }*/
      }

您可以嘗試改用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
})

這對我有用

 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