简体   繁体   English

设置jQuery中显示的Spotify响应数量的限制

[英]Setting a limit to the number of Spotify responses displayed in jQuery

I'm building something using the Spotify Search API. 我正在使用Spotify搜索API进行构建。 What I have at the minute is an input field which searches the Spotify database each time there is a key up and displays the nearest 10 results. 我现在只有一个输入字段,每次有按键输入时,它都会搜索Spotify数据库并显示最近的10个结果。

The thing I am struggling with is that I want to limit the number of results to just 10, which I have, but I've obviously got the counter in the wrong place, which means that each time there is a key up, another 10 results are added to the results list. 我正在努力解决的问题是,我希望将结果数限制为只有10个,但是我显然将计数器放在错误的位置,这意味着每次输入一个键时,都会有10个结果将添加到结果列表中。

jQuery(document).ready(function($) {

    var searchResult = null;  

    $('.spotify-input').each(function() { 

        var elem = $(this);

        // Save current value of element
        elem.data('oldVal', elem.val());

        // Look for changes in the value
        elem.bind("propertychange keyup input paste", function(event) {

            // If value has changed...
            if (elem.data('oldVal') != elem.val()) {

                // Updated stored value
                elem.data('oldVal', elem.val());

                // Do action
                query = $(this).val();
                $.ajax({
                    url: 'http://ws.spotify.com/search/1/track.json',
                    dataType: 'json',
                    data: {q: query},
                    timeout: 30000,
                    beforeSend: function (xhr) {
                        self._activeQueryXHR = xhr;
                    },
                    complete: function (xhr, textStatus) {
                        if (self._activeQueryXHR === xhr)
                            self._activeQueryXHR = null;
                    },
                    success: function (data, textStatus, xhr) {
                        console.log(data);
                        var searchResult = data;

                        i = 0;
                        $.each(data.tracks, function(index, track) {

                            //Set the limit for the number of responses
                            if(i < 11) {
                                //Track details
                                console.log(track.name);
                                $('<li class=\"spotify-result\">'+track.name+track.artists[0].name+'</li>').appendTo($('.dropdown-list'));
                            }
                            //For each result, add +1 to the counter
                            i++;
                        })
                        //On-click Stuff
                        $('.spotify-result').click(function() {
                            $(this).clone().appendTo($('.selected-list'));
                        });
                    }
                }) 

            }
        });

    });
});

I hope I've explained what I require. 我希望我已经解释了我的要求。 I'm not a Javascript guy so apologies if the code is poor. 我不是Java语言专家,如果代码不好,我会道歉。

Here you need to empty .dropdown-list container in each success method call 在这里,您需要在每个成功方法调用中清空.dropdown-list容器

$('.dropdown-list').empty();
 $.each(data.tracks, function(index, track) {

                            //Set the limit for the number of responses
                            if(i < 11) {
                                //Track details
                                console.log(track.name);
                                $('<li class=\"spotify-result\">'+track.name+track.artists[0].name+'</li>').appendTo($('.dropdown-list'));
                            }
                            //For each result, add +1 to the counter
                            i++;
                        })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM