質問

私はそうとしていることを、決して綺麗というレベルへの集計mousemoveイベントになることを確保している私のコードが呼び出され、毎250-300ミリ秒です。

私は考えていたのを以下のようなものでした疑問があった場合はより良いパターンの違いによるものなのか、それともjQueryを提供すると同じことを行ない:

var mousemove_timeout = null;

$('body').mousemove(function() {
  if (mousemove_timeout == null) {
    mousemove_timeout = window.setTimeout(myFunction, 250);
  }
});

function myFunction() {
  /*
   * Run my code...
   */

  mousemove_timeout = null;
}

編集: の受け答え以下のよう仕事を完璧にこうした状況しかし、ことを確認いただくことが mousestop() 機能性の応答を実際に解消私の凝集力していただけ読むことをお探しの回答はしばらく時間がかかる場合は、 mousestop プラグインは何をす!

役に立ちましたか?

解決

コードはきれ クリアタイムアウト 設定する前にnullまたはこの漏えい:

window.clearTimeout(mousemove_timeout);
mousemove_timeout = null;

の代替として使えmousemove/mousestop 併せて ウインドウです。setInterval

var timer = null;
var isIntervalSet = false;

$('body').mousemove(function() {
    if (isIntervalSet) {
        return;
    }
    timer = window.setInterval(function() {
        /*
        * Run my code...
        */    
    }, 250);
    isIntervalSet = true;
}).mousestop(function() {
    isIntervalSet = false;
    window.clearTimeout(timer);
    timer = null;
});

他のヒント

まった解の受け答えを見つけた場合には、マウスを移動し続け、常に、特に円運動mousemove()イベントが発火し続け、マウスの座標は同じです。ったより簡単なソリューションを解消mousestop()およびsetTimeout.

$("body").mousemove(function (e) {
        if (enableHandler) {
            handleMouseMove(e);
            enableHandler = false;
        }
});

timer = window.setInterval(function(){
    enableHandler = true;
}, 100);

これは正しく通話handleMouseMove()で、約100ms.(注ったので時間の遅れおよび間隔でJavaScriptは実時間保証)

解決策とう^^

うことなくグローバルvar.は、適当な溶液とはなんですか?

$(function() {
    $("#foo").mousemove((function() {
        var timer = null;

        return function() {
            if (timer !== null) {
                window.clearTimeout(timer);
            }
            timer = window.setTimeout(foo, 250);
        };
    })());
});

function foo() {
    //...
}

このゲマウスの位置のカスタム期間のmiliseconds

var timer;
var refresh_time = 50;
var x = 0;
jQuery('body').mousemove(function(evt) {
  if (timer)
    clearTimeout(timer);
    timer = setTimeout(function(){
      var mouse_x = evt.clientX;
      if(mouse_x != x){
        x = mouse_x;
        console.log('mouse is on a new x position' + x);    
      }
    }, refresh_time);        
})

お探しのコード側/Debouncing.

http://benalman.com/projects/jquery-throttle-debounce-plugin/ http://drupalmotion.com/article/debounce-and-throttle-visual-explanation

サンプルからunderscore.jS

// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
function debounce(func, wait, immediate) {
    var timeout;
    return function() {
        var context = this, args = arguments;
        var later = function() {
            timeout = null;
            if (!immediate) func.apply(context, args);
        };
        var callNow = immediate && !timeout;
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
        if (callNow) func.apply(context, args);
    };
};

かんしょうパーティーにも使用人が訪れる人がこのスレッドが、ここは私の2つのセント.

の弾性率オペレーターと簡単な数を単位ときスロットルの発射速度の機能最小限のパフォーマンスのようにしていただくことになります。

var fired = 0;
$('#element').on('mousemove', function(){
   fired++;
   // Fire 5x less than usual
   if(!(fired % 5) || fired == 1) yourFunction();
})

また、い恐れの最大整数限定でリセットすることができ、火力変数毎にX万ヒットを再度、弾性率オペレーター)またはmouseoutイベントです。

これは、本当にとても興味深い質問です。私は、hackishないということをチェックできるこ ライブデモ 以下のスニペット:

({
    event: null,
    interval: null,
    init: function(){
        var self = this;
        $(document).bind("mousemove", function(e){self.event=e;});
        this.interval = setInterval(function(){
            /** do what you wish **/
            console.log(self.event);
        }, 250);
        return this;
    },
    stop: function(){
        $(document).unbind("mousemove", this.event);
        clearInterval(this.interval);
    },
}).init();

いつでも保存でき数行のタイムアウトがnullの場合、タイマー:

var paused = null;

$("body").mousemove(function (e) {
    if (!paused){
        /** your code here **/
        paused = setTimeout(function(){paused=null}, 250);
    }
});

なぜ使用しないで setInterval() の代わりにタイムアウト?

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top