I wrote this code. Which sends input values to the API after pressing enter. I tried to use array, but the code causes duplicates. But duplicates aren't the only problem. Some values are not added to the array. Always 1 value after the first press of enter. Dropping values and duplicates occur regardless of API status.
<input type="text" name="qr" class="search form-control"/>
$(document).ready(function() {
var exist_qrs = new Set();
// I try use array but code duplicate elements
$("input").on("keydown", function search(e) {
if (e.keyCode == 13) {
input_qr = $(this).val();
$.ajax({
url: "API URL",
type: "POST",
dataType: 'json',
data: {
qr: input_qr
},
success: function(response) {
if (response[8] == 1) {
// response[8] - status from api
exist_qrs.add(input_qr);
console.log(exist_qrs)
}
}
})
$(this).val('')
}
});
});