Jquery中的嵌套IF - 正确的javascript语法

时间:2016-04-16 13:03:29

标签: javascript jquery arguments

尝试在var msg语句中查找如何从此jquery函数传递IF个参数结果:

$(document).mousemove(function( event ) {
var msg = "Handler for .mousemove() called at ";
msg += event.pageX + ", " + event.pageY;
if (msg->event.pageX > '750'){
$( "‪#‎log‬" ).append( "<div>" + msg + "</div>" );
}});

我做错了什么?

2 个答案:

答案 0 :(得分:1)

  

我想创建一些可以使用变量的热图   并在以后操作它们,这将是什么方法来解释这个

像这样的东西

$(document).mousemove(function( event ) {
 var map=   {} ;
 var msg = event.pageX + ", " + event.pageY;
  map.x = event.pageX - 0; 
  map.y = event.pageY - 0; 
  if ( map.x > 750)
  {
    var msg = "Handler for .mousemove() called at ";
    $( "‪#‎log‬" ).append( "<div>" + msg + "</div>" );
  }
});

答案 1 :(得分:0)

我建议你看一下debounce,因为mousemove事件经常被触发很多很快,所以你可能会这样写:

// from https://github.com/m-gagne/limit.js/blob/master/limit.js
Function.prototype.debounce = function (milliseconds, context) {
  var baseFunction = this,
      timer = null,
      wait = milliseconds;

  return function () {
    var self = context || this,
        args = arguments;

    function complete() {
      baseFunction.apply(self, args);
      timer = null;
    }

    if (timer) {
      clearTimeout(timer);
    }

    timer = setTimeout(complete, wait);
  };
};

$(function () {
  $(document).on('mousemove', function(event) {
    if (event.pageX > 150){
      $('#log').append( '<p>' + 'Handler for .mousemove() called at ' + event.pageX + ', ' + event.pageY + '</p>');
    }
  }.debounce(5)); // debounce with a 5 millisecond limit

});
<script src="https://code.jquery.com/jquery-1.12.1.min.js"></script>

<div id="log">

</div>