S
S
shnicel2015-11-30 20:46:24
JavaScript
shnicel, 2015-11-30 20:46:24

How to make an ajax request?

Hello everyone, there is some code, not when I didn’t work with ajax, I want to make the question about 70% when scrolling down, so that the content would be loaded with the offset parameters first 0, then 50,100, etc. now it's all in this form

if ($(this).hasClass('group')) {
            //$('.bitrate').children('div').css('display', 'none');
            VK.callMethod("resizeWindow", 800, 1275);
            VK.api('audio.get', { owner_id :-1959, offset:0, count: 50}, function(pl_music2) {
                if(pl_music2.response) {
                    $.each(pl_music2.response, function(key, value) {
                        key != 0 ? line += '<a id="pg'+key+'" href="'+value.url+'" class="sm2_link">'+value.artist+' - '+value.title+'</a>' : false;
                    });
                    $('.vkla.group-detail').html(line);
                    $('div[for="group-detail"]').trigger('click');
                }
            });
        }

When you click on a tab, a query is executed, 50 elements are displayed, offset: 0

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Semyon, 2015-11-30
@skamenetskiy

It can be something like this, it is worth modifying if that.

if ($(this).hasClass('group')) {
    VK.callMethod("resizeWindow", 800, 1275);
    var offset = 0,
        $body = $('body'),
        getData = function(offset) {
            VK.api('audio.get', {
                owner_id: -1959,
                offset: 0,
                count: 50
            }, function(pl_music2) {
                if (pl_music2.response) {
                    $.each(pl_music2.response, function(key, value) {
                        key != 0 ? line += '<a id="pg' + key + '" href="' + value.url + '" class="sm2_link">' + value.artist + ' - ' + value.title + '</a>' : false;
                    });
                    $('.vkla.group-detail').html(line);
                    $('div[for="group-detail"]').trigger('click');
                }
            });
        };
    getData(offset);
    $body.on('scroll', function() {
        if ((($body.scrollTop() / $body.height()) * 100) > 70) {
            offset += 50;
            getData(offset);
        }
    });
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question