Welcome to the world of Vue js! I'm currently working with some code in Para.vue that looks like this:
Para.vue
<template>
<t-row>
<t-col :span="13">
<t-input
:id="id+'_tam'"
ref="tam"
v-model="ayristirilmisPara.tam"
reverse
:label="label"
:disabled="disabled"
name="Oran *"
:labelSpan="15"
:inputSpan="8"
:maxlength="tamMaxLength"
:vNumber="true"
@input="updateTam"
v-validate="{ required: this.isRequired }"
:error="errors.first('Oran *')"
class="para"
/>
</t-col>
<t-col :span="1" style="padding-left: 0px; padding-right: 0px; padding-top: 12.5px;">,</t-col>
<t-col :span="10">
<t-input
ref="kesir"
:id="id+'_kesir'"
v-model="ayristirilmisPara.kesir"
:maxlength="kesirMaxLength"
:vNumber="true"
:disabled="disabled"
:name="'Oran Kesir *'"
:labelSpan="0"
:inputSpan="18"
label
@input="updateKesir"
v-validate="{ required: this.isRequired }"
:error="errors.first('Oran Kesir *')"
class="para"
/>
</t-col>
<t-col :span="1"></t-col>
</t-row>
</template>
<script>
export default {
props: {
tamMaxLength: {
type: Number,
default: 3
},
kesirMaxLength: {
type: Number,
default: 2
},
value: {
type: [String, Number],
default: "0.00"
},
label: {
type: String,
default: "",
required: false
},
isRequired: {
type: Boolean,
default: false,
required: false
},
disabled: {
type: Boolean,
required: false,
default: false
}
},
data() {
return {
tam: "0",
kesir: "0"
};
},
methods: {
updateTam(tam) {
if (!tam) tam = "";
this.tam = tam;
this.$emit("input", `${tam}.${this.kesir}`);
},
updateKesir(kesir) {
if (!kesir) kesir = "";
this.kesir = kesir;
this.$emit("input", `${this.tam}.${kesir}`);
}
},
computed: {
ayristirilmisPara() {
if (this.value === undefined || this.value === null) this.value = "0.0";
const paraParcali = this.value.toString().split(".");
let tutar = {
tam:
paraParcali[0] == null
? 0
: paraParcali[0] || paraParcali[0] == ""
? 0
: paraParcali[0],
kesir:
paraParcali[1] == null
? 0
: paraParcali[1] || paraParcali[1] == ""
? 0
: paraParcali[1]
};
this.tam = tutar.tam;
this.kesir = tutar.kesir;
this.$emit("input", `${tutar.tam}.${tutar.kesir}`);
return tutar;
}
}
};
</script>
<style>
.el-input {
min-width: 45px;
}
.para .el-input__inner {
padding: 0px;
}
</style>
The current functionality works perfectly fine by automatically inserting 0 when a user attempts to delete all content from the input areas with id'+_tam' and id+'_kesir'. However, now you want to implement a feature where if the user doesn't enter any input within 3 seconds, it should automatically insert 0. You tried using setInterval in the computed property but it didn't work as expected. Here's how you can achieve this:
computed: {
ayristirilmisPara() {
let timer = setTimeout(function() {
if (this.value === undefined || this.value === null) this.value = "0.0";
const paraParcali = this.value.toString().split(".");
let tutar = {
tam:
paraParcali[0] == null
? 0
: paraParcali[0] || paraParcali[0] == ""
? 0
: paraParcali[0],
kesir:
paraParcali[1] == null
? 0
: paraParcali[1] || paraParcali[1] == ""
? 0
: paraParcali[1]
};
this.tam = tutar.tam;
this.kesir = tutar.kesir;
this.$emit("input", `${tutar.tam}.${tutar.kesir}`);
return tutar;
}.bind(this), 3000);
}
}
Give this a try and see if it solves your issue. Thank you for reaching out!