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

115
Vistas
how to stop a function by li

I need to make when i click in the body the li function returns i did logo id just for test

let li = document.getElementById("li");
let ul = document.getElementById("ul");
let img = document.getElementById("img");
let logo = document.getElementById("logo");
var cancelled;

// I want clicking on the body to stop this function
let i = li.onclick = setInterval(function() {
  ul.style.transition = ".5s";
  ul.style.visibility = "visible";
  li.style.color = "var(--Almost-Black)";
  img.style.transition = ".5s";
  img.style.transform = "rotate(180deg)";
  ul.style.top = "30px";
}, 1000)

logo.onclick = function() {
  clearInterval(i)
}
<div class="header">
  <div class="container">
    <nav>
      <div class="logo" id="logo">
        <img src="images/logo.svg" alt="">
      </div>
      <ul>
        <li class="li" id="li"><a href="#">Features</a> <img src="images/icon-arrow-down.svg" id="img">
          <ul class="ul" id="ul">
            <li><img src="images/icon-todo.svg" alt=""> To do list</li>
            <li><img src="images/icon-calendar.svg" alt=""> calendar</li>
            <li><img src="images/icon-reminders.svg" alt=""> Reminders</li>
            <li><img src="images/icon-planning.svg" alt=""> planning</li>
          </ul>
        </li>
        <li class="li"><a href="#">Company</a></li>
        <li class="li"><a href="#">Careers</a></li>
        <li class="li"><a href="#">About</a></li>
      </ul>
      <div class="btns">
        <button class="login"><a href="#">login</a></button>
        <button class="reg"><a href="#">Register</a></button>
      </div>
    </nav>
  </div>
</div>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The value of li.onclick must be a function. You should assign the interval to i inside the function, not when assigning onclick. You should also clear the old interval first, in case it's clicked twice.

If you want to click on the body instead of the logo to cancel, use document.body.onclick. You also need to call event.stopPropagation() in the li.onclick so that clicking on the LI doesn't bubble out to the body.

let li = document.getElementById("li");
let ul = document.getElementById("ul");
let img = document.getElementById("img");
let logo = document.getElementById("logo");
var cancelled;
let i;

// I want clicking on the body to stop this function
li.onclick = function(event) {
  event.stopPropagation();
  clearInterval(i);
  i = setInterval(function() {
    ul.style.transition = ".5s";
    ul.style.visibility = "visible";
    li.style.color = "var(--Almost-Black)";
    img.style.transition = ".5s";
    img.style.transform = "rotate(180deg)";
    ul.style.top = "30px";
  }, 1000);
};

document.body.onclick = function() {
  clearInterval(i)
}
<div class="header">
  <div class="container">
    <nav>
      <div class="logo" id="logo">
        <img src="images/logo.svg" alt="">
      </div>
      <ul>
        <li class="li" id="li"><a href="#">Features</a> <img src="images/icon-arrow-down.svg" id="img">
          <ul class="ul" id="ul">
            <li><img src="images/icon-todo.svg" alt=""> To do list</li>
            <li><img src="images/icon-calendar.svg" alt=""> calendar</li>
            <li><img src="images/icon-reminders.svg" alt=""> Reminders</li>
            <li><img src="images/icon-planning.svg" alt=""> planning</li>
          </ul>
        </li>
        <li class="li"><a href="#">Company</a></li>
        <li class="li"><a href="#">Careers</a></li>
        <li class="li"><a href="#">About</a></li>
      </ul>
      <div class="btns">
        <button class="login"><a href="#">login</a></button>
        <button class="reg"><a href="#">Register</a></button>
      </div>
    </nav>
  </div>
</div>

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