Home > Back-end >  Run two functions on one button click in JS/VueJS
Run two functions on one button click in JS/VueJS

Time:11-16

Im trying to run two functions; one being a 'reload page' and the other a 'copy to clipboard'. I have tried all the options on this page:

How to call multiple functions with @click in vue?

but none seem to work for me. Can anyone give me any pointers here? Any help is appreciated.

<button @click="reloadPage(); 
   copytheURL(URLcopyaddress);">Copy</button>



methods: {
    //reloads the page
    reloadPage() {
      window.location.reload();
    },
 //copy to clipboard
    async copytheURL(s) {
      await navigator.clipboard.writeText(s);
      alert("URL Copied!");
    },
},

CodePudding user response:

Why don't you put window.location.reload(); inside the async copytheURL(s)? Right after alert("URL Copied!"); ?

CodePudding user response:

The problem is the reloading the page stops the javaScript run. replace the order, call the copy first and you should be fine

CodePudding user response:

Try like this below it will work like magic. The problem with your code is you are trying call both functions in synchronus fashion. But ideally you should wait for the async function to complete and perform your synchronous function.

<button @click="copytheURL(URLcopyaddress).then(reloadPage)">Copy</button>
  • Related