Question

UPDATED QUESTION WITH ANSWER!!

Original Question

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 a 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?

ANSWER

It appears I was taking the wrong approach by wanting to use object.watch - with help from paul_wilkins over at SitePoint, he showed me a method to store information using jQuery data (http://api.jquery.com/data). Also simplifying my equation too haha

Here is the new code:

<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);

                block = x + (y * 200) - 200;

                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 );

                if (block != $('#gdimage').data('storedBlock')) {

                    $.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').data('storedBlock', block);

            $("#gdimage").click( function( eventObj ) {
                window.location = "/redirect/?x=" + x + "&y=" + y + "";
            });

        });

    });

</script>
Was it helpful?

Solution

It appears I was taking the wrong approach by wanting to use object.watch - with help from paul_wilkins over at SitePoint, he showed me a method to store information using jQuery data (http://api.jquery.com/data). Also simplifying my equation too haha

Here is the new code:

<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);

                block = x + (y * 200) - 200;

                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 );

                if (block != $('#gdimage').data('storedBlock')) {

                    $.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').data('storedBlock', block);

            $("#gdimage").click( function( eventObj ) {
                window.location = "/redirect/?x=" + x + "&y=" + y + "";
            });

        });

    });

</script>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top