2013-04-22 50 views
0

我的應用程序是aps.net MVC,是我使用ajax加載幾個圖像,通過獲得鼠標Y位置,它的工作原理,但如果我將鼠標移動超過一個像素,例如100它加載所有圖像一次!爲了測試腳本,我添加了Alert,並且當用戶點擊ok時,圖像按照它應該做的那樣前進。我試過setInterval和Timeout,都沒有工作。這裏是我的代碼:警報功能 - Javascript作爲等待函數

$("#container2").bind('mousemove', function (e) { 

     lastX = e.pageX - position.x; 
     lastY = e.pageY - position.y; 
     coordinate = "x=" + lastX + ", y=" + lastY; 
     $('#pValue').val(coordinate); 
     $('#lastX').val(lastX); 
     $('#lastY').val(lastY); 

     waitStep = lastY >= 0 ? 1 : -1; 
     waitInc = waitStep; 
     waitCounter = lastY; 

     for (var n = 0; n < Math.abs(lastY); n += 1) { 
      // imageSequence[n] = new Image(); 
      dicom1.src = '/Home/GenerateImage?' + $.param({ 
       pX: pointXF, 
       pY: pointYF, 
       pZ: waitInc * sThickness 
      }); 
      waitInc = waitInc + waitStep; 
      alert(waitInc); 


     } 

希望你的建議,在此先感謝。

+0

你的代碼中顯式迭代,並且將圖像加載在Y方向移動的每個像素。如果你不想這樣做,爲什麼要這樣編碼? – Pointy 2013-04-22 20:49:21

+0

這是一個3D DICOM圖像,我通過在畫布上上下移動鼠標來更改Z軸;它可以工作,但跳到最後一個位置而不顯示其他圖層。 – hncl 2013-04-22 21:21:59

回答

1

嘗試:

var speed = 15; 
var timeout = 0; 
$("#container2").bind('mousemove', function (e) { 
    if (!timeout) { 
     timeout = setTimeout(function(){ 
      timeout = 0; 
      lastX = e.pageX - position.x; 
      lastY = e.pageY - position.y; 
      coordinate = "x=" + lastX + ", y=" + lastY; 
      $('#pValue').val(coordinate); 
      $('#lastX').val(lastX); 
      $('#lastY').val(lastY); 

      waitStep = lastY >= 0 ? 1 : -1; 
      waitInc = waitStep; 
      waitCounter = lastY; 

      for (var n = 0; n < Math.abs(lastY); n += 1) { 
       // imageSequence[n] = new Image(); 
        dicom1.src = '/Home/GenerateImage?' + $.param({ 
        pX: pointXF, 
        pY: pointYF, 
        pZ: waitInc * sThickness 
       }); 
       waitInc = waitInc + waitStep; 
       alert(waitInc); 
      } 
     }, speed); 
    } 
}); 
+0

謝謝,不幸沒有工作。 – hncl 2013-04-22 21:19:22

0

要在15執行一次代碼,你可以嘗試:

var speed = 0; 
$("#container2").bind('mousemove', function (e) { 
    ++speed; 
    if (speed % 15 == 0) { 
      lastX = e.pageX - position.x; 
      lastY = e.pageY - position.y; 
      coordinate = "x=" + lastX + ", y=" + lastY; 
      $('#pValue').val(coordinate); 
      $('#lastX').val(lastX); 
      $('#lastY').val(lastY); 

      waitStep = lastY >= 0 ? 1 : -1; 
      waitInc = waitStep; 
      waitCounter = lastY; 

      for (var n = 0; n < Math.abs(lastY); n += 1) { 
       // imageSequence[n] = new Image(); 
        dicom1.src = '/Home/GenerateImage?' + $.param({ 
        pX: pointXF, 
        pY: pointYF, 
        pZ: waitInc * sThickness 
       }); 
       waitInc = waitInc + waitStep; 
       alert(waitInc); 
      } 
    } 
}); 
+0

再次感謝,但它沒有奏效。我正在嘗試用等待函數替換alter。 – hncl 2013-04-23 20:44:27