Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

87
Visualizações
Custom tooltip with Vue acts weird after clicking on button two or three times

I made that code to develop a custom tooltip that auto-closes after 2 seconds from user clicking on a button. I want it to work only on click not hover .

It works well for the first two times but then it acts weird when I click on it again.

You can check it here

<template>
  <div id="app">
    <button @click="copyThat" class="relative-button"> 
    copy that 
    <div class="tooltip" v-if="copied"> copied </div>
    </button>
    
  </div>
</template>

<script>

export default {
  data(){
    return{
      copied: false
    }
  },
  methods: {
    copyThat: function (){
      this.copied = true;
      setInterval( () => {
        this.copied = false;
      }, 2000 )
    }
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
.relative-button{
    position: relative;
}
.tooltip{
    position: absolute;
    top: 130%;
    padding: 1rem;
    left: -50%;
    background: rgba(0,0,0,0.5);
    color: white;
    font-weight: bold;
    z-index: 100;
    width: 113px;
    height: 30px;
    border-radius: 4px;
    padding: 7px;
    text-transform: capitalize;
    font-weight: 400;
    font-size: 13px;
}
</style>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The problem is you doesn't clear the interval you created so once you click the copy button, it will create one new interval and several intervals will executing every 2 seconds which make your toolbar become very weird.

You could either use setTimeout or use window.clearInterval to clear all the interval before creating a new one.

about 4 years ago · Juan Pablo Isaza Relatório

0

Use setTimeout instead:

copyThat: function() {
  this.copied = true;
  setTimeout(() => {
    this.copied = false;
  }, 2000);
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda