I have a QR code creating page. I want my QR codes to be created dynamically by user input. But I don’t want to instantly create a QR code. I want to wait my user to finish writing then after one second i will generate the QR code. So I have a template like below:
<div class="app"> <qrcode-vue :value="genaratedQrCode"></qrcode-vue> <input type="text" v-model="qrCodeInput" /> </div>
And my script:
import QrcodeVue from 'qrcode.vue'; export default { data() { return { genaratedQrCode: '', qrCodeInput: '', isInputFunctionRunning: false } }, watch: { async qrCodeInput() { if (this.isInputFunctionRunning) { return; } this.isInputFunctionRunning = true; await new Promise(r => setTimeout(r, 1000)); this.genaratedQrCode = this.qrCodeInput; this.isInputFunctionRunning = false; } } components: { QrcodeVue, }, }
Apparently the code is not working. It generated the QR code every one seconds. What I want is waiting till user finished, then updating after 1 seconds.
Advertisement
Answer
You have to use .lazy
modifier :
<input type="text" v-model.lazy="qrCodeInput" />
If you want to wait some delay try this :
import QrcodeVue from 'qrcode.vue'; function debounce (fn, delay) { var timeoutID = null return function () { clearTimeout(timeoutID) var args = arguments var that = this timeoutID = setTimeout(function () { fn.apply(that, args) }, delay) } } export default { data() { return { genaratedQrCode: '', qrCodeInput: '', isInputFunctionRunning: false } }, watch: { qrCodeInput:debounce(function() { if (this.isInputFunctionRunning) { return; } this.isInputFunctionRunning = true; this.genaratedQrCode = this.qrCodeInput; this.isInputFunctionRunning = false; },1000) } components: { QrcodeVue, }, }
This is based on this answer;