I’m making a QR Code generator in Laravel.
I created a "Range" entry to set the QR code size.
I detected this with the "mouseup" feature and started creating ajax posts. (Because if I did it with "input" it would send ajax on every swipe.)
If this is done too quickly, I want to issue a warning and warn the user.
How do I detect?
this is my code:
HTML:
<input type="range" class="form-range mt-3" id="qrCodeSize" value="300" min="250" max="1000" />
JS:
$('#qrCodeSize').on('mouseup', function(){
qrCodeGenerate();
});
function qrCodeGenerate(){
let qrCodeSize = $('#qrCodeSize').val();
$.post("{{ route('qrkoduretajax') }}", {
"_token": "{{ csrf_token() }}",
qrCodeSize:qrCodeSize,
},
function(result){
if(result){
$('#qrCodeIMG').attr("src", result.qr);
}
});
I did some research but couldn’t find a solution for this.
2
Answers
Thanks to @Steve for reminding me of the "Timer" thing. This is how I solved my problem:
As soon as the input was made using "Boolean" I ran the function and set the "global variable" to "false". I used "setTimeout" to make it "true" after 1 second. I had it checked if "Global Boolean" was correct when re-entered. If 1 second elapses, "boolean" becomes "true" and allows login. If 1 second has not passed, "boolean" becomes "false" and does not allow input. Also, the previous value is re-entered.
Sorry for my English. Thanks everyone.
Basically you have two main options:
You can also combine the two if you prefer that
Server-side
Your server (Laravel) in this case can use rate-limiting on your Laravel server which will protect your server from abuse even if it is not coming from your UI. Your client-side could receive some message upon which you could let your user know that he/she abused the app.
Client-side
The problem boils down to managing that an action does not happen too frequently. This is a neat way to do it:
The snippet above is agnostic to request sending, basically it delegates it to a function
f
and it is focusing on the mechanism that you need. Feel free to replacef
with the function you use for sending requests.