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

416
Visualizações
How to make text blink conditionally using java script

On a web page I have some text that I wish to blink under certain conditions (when counter is even) and not to otherwise

The problem is that once it starts blinking, it blinks all of the time. How can I stop it?

<head>
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.7.1.min.js"></script>
    <title>Test blink</title>
</head>

<div id="wait-text">&nbsp</div>
<button onclick="updateText()">Click</button>

<script>
    var counter = 0;
    var wait_text = document.getElementById('wait-text');

    function updateText() {
        counter ++;
        wait_text.innerHTML = `wait ${counter}`;
        wait_text.className = '';
        if (counter % 2 == 0) {
            wait_text.innerHTML = `blink ${counter}`;
            wait_text.className = 'blinking';
        }
    }

    function blinker() {
        let fadeout = 1500;
        let fadein = 750;
        $('.blinking').fadeOut(fadeout);
        $('.blinking').fadeIn(fadein);
    }
    setInterval(blinker, 500);
</script>
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

  • I have created a class blink which contains CSS for blinking effect.
  • then I am checking whether counter is odd or even.
  • if it's even then I am adding blink class and if it's not than I am checking whether it's contains blink class or not. If it does then I am removing blink class otherwise do nothing.

const text = document.querySelector("#wait-text");
let counter = 0;
const updateText = () => {
  counter++;
  text.innerHTML = counter;
  if (counter % 2 == 0) {
    text.classList.add("blink");
  } else {
    if (text.classList.contains("blink")) {
      text.classList.remove("blink");
    }
  }
}
.blink {
  animation: blinkIt 1s infinite;
}

@keyframes blinkIt {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
<div id="wait-text">&nbsp</div>
<button onclick="updateText()">Click</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

You can store the interval id and call clearInterval on each button click, only starting the interval again when the counter is even. Furthermore, to stop all current and queued animations on the element, call $('.blinking').stop(true, true).

var counter = 0;
var wait_text = document.getElementById('wait-text');
let intvlId;

function updateText() {
    counter++;
    clearInterval(intvlId);
    $('.blinking').stop(true, true).show();
    wait_text.innerHTML = `wait ${counter}`;
    wait_text.className = '';
    if (counter % 2 == 0) {
        wait_text.innerHTML = `blink ${counter}`;
        wait_text.className = 'blinking';
        intvlId = setInterval(blinker, 500);
    }
}

function blinker() {
    let fadeout = 1500;
    let fadein = 750;
    $('.blinking').fadeOut(fadeout);
    $('.blinking').fadeIn(fadein);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="wait-text">&nbsp;</div>
<button onclick="updateText()">Click</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use .classList.toggle()

<head>
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.7.1.min.js"></script>
    <title>Test blink</title>
</head>

<div id="wait-text">&nbsp</div>
<button onclick="updateText()">Click</button>

<script>
    var counter = 0;
    var wait_text = document.getElementById('wait-text');

    function updateText() {
        counter ++;
        wait_text.innerHTML = `wait ${counter}`;
        wait_text.className = '';
        if (counter % 2 == 0) {
            wait_text.innerHTML = `blink ${counter}`;
            wait_text.classList.toggle("blinking"); //chang here
        }
    }

    function blinker() {
        let fadeout = 1500;
        let fadein = 750;
        $('.blinking').fadeOut(fadeout);
        $('.blinking').fadeIn(fadein);
    }
    setInterval(blinker, 500);
</script>
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