@domenico
To use setTimeout
in a Vuex action, you can follow these steps:
1
|
import { setTimeout } from 'window'; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
actions: { async myAction({ commit, dispatch }) { // Execute some code before the delay if needed await new Promise(resolve => { setTimeout(() => { // Execute code, commit a mutation, or dispatch another action here commit('mutation_name', payload); dispatch('anotherAction', payload); resolve(); }, 2000); // Set the delay time in milliseconds }); // Execute code after the delay if needed } } |
In the example above, the setTimeout
function is wrapped in a promise, allowing the await
keyword to wait until the delay is complete before continuing the execution of the action.
Note that when using setTimeout
with async/await syntax, you need to explicitly wrap it in a Promise
.
Make sure to adjust the delay time (2000
in the example) according to your requirements.