I have an infinite loop in which multiple QR markers are being detected from a live video feed. I want the QR code IDs being sent to the server via ajax. This is how I do it:
while (true) {
var markers = detect(imageData);
$.ajax({
url: '/upload',
data: { QRCodes: markers.id },
type: 'POST',
success: function(response){
console.log(response);
},
error: function(error){
console.log(error);
}
});
}
The problem with this approach is a bottleneck in the ajax requests. When for example, a QR code with an id "12" is detected, it rapidly posts "12 12 12 12 12...", which is too fast for ajax to send to the server. Whenever new QR codes are detected it causes a huge delay until the most recent QR marker is being received on the server.
Thus, I would ajax to only accept a new request every 0.2 seconds. Meanwhile, all other requests outside of this "delay" should be just ignored. Or, instead of a delay I like the ajax to only send the newest QR marker (Last in First Out).
I found multiple similar posts but they answered how to "delay" a request by x seconds. However, I dont want a request to be delayed but always need the newest QR id number on the server.
Aucun commentaire:
Enregistrer un commentaire