Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3957882
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:32:42+00:00 2026-05-20T02:32:42+00:00

UPDATED QUESTION WITH ANSWER!! Original Question I’ve been looking at Eli’s object.watch (https://gist.github.com/384583) script

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T02:32:43+00:00Added an answer on May 20, 2026 at 2:32 am

    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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.