0

I tried to get information about the status of some of the buttons in JSON using the $. getJson () ID with a dash is broken into two parts, and they entered into an array regarr. But I can not get data from JSON in this query:

data.regarr[0].regarr [1] //regarr is undefined

HTML:

<div class='buttons' id='lamps-fire1'>OFF</div>

My JSON:

{"lamps":{"fire1":"off","fire2":"off","fire3":"off"},"motor":"on","temperature":"12"}

JavaScript

$(document).ready(function()
{
    function reloadvalues()
    {
        $('.buttons').each(function ()
        {
                var id=$(this).attr('id');
                var re=/-/;
                var re2=/[a-z0-9]{1,}[^-][a-z0-9]{1,}/ig;
                var regarr=[];
                 regarr=id.match(re2);
                if (id.search(re)==-1)
                {
                    $.getJSON('homeapi.ini','tire=none&id='+encodeURIComponent(id),function (data)
                    {
                    if (data.motor=='off')
                    {
                        $(this).html('OFF.');
                    }
                    else{
                        $(this).html('ON.');
                    }
                    });
                }
                else{
                    $.getJSON('homeapi.ini','',function (data)
                    {
                    if ((regarr[1]!='undefined')||(regarr[0]!='undefined')||(regarr !='undefined'))
                    {
                    if (data.regarr[0].regarr[1]=='off')
                    {
                        $(this).html('OFF.');
                    }
                    else{
                        $(this).html('ON.');
                    }
                    }
                });
                }
            });
    }
    setInterval(function (){
        reloadvalues();
        },5000);
});

Maybe someone knows what went wrong?

1
  • wait there's not key regarr in your data JSON is there? you probably want domething like data[regarr[0]][regarr[1]]... Commented Aug 7, 2012 at 11:02

1 Answer 1

1

The reference this is not the same within the callback function scope as it is within the each loop. You will have to cache a reference to it before entering the sub-functions. Then, within your callback function blocks in your $.getJSON method, use the stored reference (in this case I called it self) instead of this:

$(document).ready(function() {
    function reloadvalues() {
        $('.buttons').each(function() {
            var self = this;

            /* ... */ 
            if (id.search(re) == -1) {
                $.getJSON('homeapi.ini', 'tire=none&id=' + encodeURIComponent(id), 
                function(data) {

                    if (data.motor == 'off') {
                        $(self).html('OFF.');
                    }
                    else {
                        $(self).html('ON.');
                    }
                });
            }
            /* ... */
        });
    }
});​​​

Regarding the reference error, when using property names stored in variables, you have to de-reference the json object using [ ] rather than a simple .. In this case:

Replace data.regarr[0].regarr[1] with data[regarr[0]][regarr[1]]

Sign up to request clarification or add additional context in comments.

2 Comments

Hello.But now in Firebug:TypeError: data.regarr is undefined if (data.regarr[0].regarr[1]=='off'). Nothing has changed:(
No errors! But I have a problem. When I change the JSON data to the server, the changes do not appear on the page. Most likely I'm doing something wrong. Here is my code: pastebin.com/AyT2b8hy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.