Barcode scanner

i have the same problem like this https://stackoverflow.com/questions/39449293/how-to-configure-ajax-select2-for-working-with-a-barcode-scanner .
this is my code :

  $("#kode-barang").select2({
    ajax: {
        url: base_url + 'barang/kodebarang_autocom/',
        dataType: "json",
        delay: 0,
        data: function (params) {
            return {
                kode: params.term
            };
        },
        processResults: function (data) 
        {
            var results = [];
            $.each(data, function (index, item) {
                results.push({
                    id: item.id_barang,
                    text: item.kode_barang,
                });
            });
            return {
                results: results
            };
        },
        cache: true
    },
    language: "id",
    theme: "bootstrap4",
});

There were several solutions offered in the StackOverflow post. Did you try any of them?

I think the problem here is that the Select2 Ajax feature sends a query to the server as soon as the user has entered a couple of characters. There are a couple of ways I can think of that might resolve this problem:

  1. If the barcode scanner always outputs values of the same length, set the Select2’s “minimumInputLength” configuration option to that size.
  2. Use the " ajax.delay" option of the Select2 Ajax feature to make Select2 wait longer before sending the query.

Check out the Select2 documentation for more details on these configuration options.