将object.watch纳入当前脚本?困惑

时间:2011-02-03 18:05:43

标签: javascript variables object watch

更新的问题!!

原始问题

我一直在看Eli的object.watch(https://gist.github.com/384583)脚本,我理解它的概念以及它的作用,但我对如何实际使用它感到困惑在我的脚本!即使这对大多数人来说似乎很明显,我只是没有看到它:S

我甚至可能完全尝试错误的方法而且object.watch不是我真正需要使用的!

我这里有一个脚本:

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

现在您可以看出,服务器负载将非常高,鼠标移动通过AJAX不断调用fetch.php页面中的数据。所以,我想要做的只是在变量“block”改变它的值时调用AJAX。

所以我假设,我必须在某处存储一个值,然后当值更改时使用存储的值检查它,但当然,存储的值也将始终更改为新值,因为它全部由不断变化确定变量

ANSWER

看来我采用错误的方法想要使用object.watch - 在SitePoint的paul_wilkins的帮助下,他向我展示了一种使用jQuery数据存储信息的方法(http://api.jquery.com/data )。也简化了我的等式哈哈

这是新代码:

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

1 个答案:

答案 0 :(得分:0)

看来我采用错误的方法想要使用object.watch - 在SitePoint的paul_wilkins的帮助下,他向我展示了一种使用jQuery数据存储信息的方法(http://api.jquery.com/data )。也简化了我的等式哈哈

这是新代码:

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