Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

413
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda