I’ve been looking at Eli’s object.watch (https://gist.github.com/384583) script and I understand the idea of it and what it does but I’m so confused about how I actually use it in my script! Even if this seems quite obvious to most, I’m just not seeing it :S
I might even be trying the wrong approach to it entirely and object.watch isn’t what I actually need to be using!
I’ve got my script here:
<script type="text/javascript">
jQuery.fn.elementlocation = function() {
var curleft = 0;
var curtop = 0;
var obj = this;
do {
curleft += obj.attr('offsetLeft');
curtop += obj.attr('offsetTop');
obj = obj.offsetParent();
} while ( obj.attr('tagName') != 'BODY' );
return ( {x:curleft, y:curtop} );
};
$(document).ready( function() {
$("#gdimage").mousemove( function( eventObj ) {
var location = $("#gdimage").elementlocation();
var x = eventObj.pageX - location.x;
var y = eventObj.pageY - location.y;
x = x / 5;
y = y / 5;
x = (Math.floor( x ) + 1);
y = (Math.floor( y ) + 1);
if (y > 1) {
block = (y * 200) - 200;
block = block + x;
} else {
block = x;
}
x = ((x * 2) + (x*3)) - 4;
y = ((y * 2) + (y*3)) - 4;
x = (Math.floor( x ));
y = (Math.floor( y ));
$("#block").text( block );
$("#x_coords").text( x );
$("#y_coords").text( y );
$.ajax({
type: "GET",
url: "fetch.php",
data: "x=" + x + "&y=" + y + "",
dataType: "json",
async: false,
success: function(data) {
$("#user_name_area").html(data.username);
}
});
$("#gdimage").click( function( eventObj ) {
window.location = "/redirect/?x=" + x + "&y=" + y + "";
});
});
});
</script>
Now as you can tell, the server load will be pretty high with the mouse move constantly calling data from the fetch.php page through AJAX. SO, what I’m trying to do is only call upon the AJAX when the variable “block” changes it’s value.
So I assume, I have to somewhere store a value, and then when the value changes check it with the stored value, but of course, the stored value will always change to the new value too since it’s all determined by constantly changing variables?
So how would I get it to only call the AJAX section when the block number changes?