javascript - 在 select2 插件中调用 ajax 时引入延迟

标签 javascript jquery delay jquery-select2

我正在使用来自 http://ivaynberg.github.io/select2/ 的 select 2 示例 我在此页面中使用“加载远程数据”示例。

问题:只要我输入一个字母,系统就会进行 ajax 调用。我想在此请求期间引入 1 秒的延迟,这将允许用户键入他的搜索字符串。

我正在从站点添加代码。请让我知道如何引入延迟。

("#e6").select2({
            placeholder: "Search for a movie",
            minimumInputLength: 1,
            ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
                url: "http://api.rottentomatoes.com/api/public/v1.0/movies.json",
                dataType: 'jsonp',
                data: function (term, page) {
                    return {
                        q: term, // search term
                        page_limit: 10,
                        apikey: "ju6z9mjyajq2djue3gbvv26t" // please do not use so this example keeps working
                    };
                },
                results: function (data, page) { // parse the results into the format expected by Select2.
                    // since we are using custom formatting functions we do not need to alter remote JSON data
                    return {results: data.movies};
                }
            },
            initSelection: function(element, callback) {
                // the input tag has a value attribute preloaded that points to a preselected movie's id
                // this function resolves that id attribute to an object that select2 can render
                // using its formatResult renderer - that way the movie name is shown preselected
                var id=$(element).val();
                if (id!=="") {
                    $.ajax("http://api.rottentomatoes.com/api/public/v1.0/movies/"+id+".json", {
                        data: {
                            apikey: "ju6z9mjyajq2djue3gbvv26t"
                        },
                        dataType: "jsonp"
                    }).done(function(data) { callback(data); });
                }
            },
            formatResult: movieFormatResult, // omitted for brevity, see the source of this page
            formatSelection: movieFormatSelection,  // omitted for brevity, see the source of this page
            dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
            escapeMarkup: function (m) { return m; } // we do not want to escape markup since we are displaying html in results
        });

最佳答案

我猜想 quietMillis 属性已更改为在较新版本的 select2 中延迟:

https://select2.org/data-sources/ajax#rate-limiting-requests

$('select').select2({
  ajax: {
    url: '/example/api',
    delay: 250
  }
});

关于javascript - 在 select2 插件中调用 ajax 时引入延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18174935/

相关文章:

timer - 计时器和延迟如何在低级别工作

java - 调用之间的 Swing 延迟

javascript - 如何暂停 javascript 直到某件事完成?

javascript - Svelte/SvelteKit 'before:event'?

javascript - 为什么这个事件总是被解雇?

javascript - 查找内容并将其分配给变量

jquery - Symfony 和 Composer 组件文件夹

javascript - 使用jquery选择器计算html表中的列和行总和

javascript - 如何构建 MongoDB 查询以使用 ACL 评估允许和拒绝的权限链?

javascript - 让光标显示在输入焦点上