S
S
Sasha2942021-01-20 11:14:47
Vue.js
Sasha294, 2021-01-20 11:14:47

When a checkbox is clicked, wait for another one to be clicked or submit a request. How can this be organized?

I need to wait 1.5 seconds for another checkbox to be pressed when selecting one checkbox or send a request. How can this expectation be organized?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2021-01-20
@Sasha294

<template>
  <label>
    <input
      type="checkbox"
      @change="debouncedCheckboxChange"
      v-model="form.c1"
    />
    Check 1
  </label>
  <label>
    <input
      type="checkbox"
      @change="debouncedCheckboxChange"
      v-model="form.c2"
    />
    Check 2
  </label>
  <pre>{{ form }}</pre>
</template>

<script>
const debounce = (handle, duration = 0) => {
  let timeout = null;

  return function (...args) {
    clearTimeout(timeout);
    timeout = setTimeout(() => handle.apply(this, args), duration);
  };
};

export default {
  name: 'App',
  data() {
    return {
      form: {}
    };
  },
  methods: {
    checkboxChange() {
      this.form.date = Date.now();
    }
  },
  created() {
    this.debouncedCheckboxChange = debounce(this.checkboxChange, 1500);
  }
};
</script>

Example . Documentation .

N
Nikolay, 2021-01-20
@nickolyashka

Check the state of the desired checkbox (:checked), if it has changed, wait 1.5 seconds and fulfill your conditions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question