0
$(".title").click(function () {
  if ($(this).parent().hasClass("is-closed")) {
    $(this).parent().removeClass("is-closed");
    $(this)
      .parent()
      .css("max-height", parseInt($(this).next().height()) + parseInt(80));
    timer = window.setTimeout(() => {
      $(this).parent().css("max-height", "inherit");
    }, 500);
  } else {
    $(this).parent().addClass("is-closed");
    $(this)
      .parent()
      .css("max-height", parseInt($(this).next().height()) + parseInt(80));
    window.setTimeout(() => {
      $(this).parent().css("max-height", "50px");
    }, 2);
    window.clearTimeout(timer);
  }
});

I have an accordion menu and basically, I set max-height inherit after some time when it opened. (I do this because when resizing the browser not all content is visible. Because when minimizing the browser content has more height.)

Anyway, the code works fine. It does what I want. But in Chrome Console, I see this error:

Uncaught ReferenceError: timer is not defined

window.clearTimeout(timer); throws the error.

Is it something I should be concerned about?

Phil
  • 157,677
  • 23
  • 242
  • 245
wp-ap
  • 109
  • 1
  • 8
  • 2
    which line of your code throws that error – Bravo Jul 05 '22 at 01:12
  • window.clearTimeout(timer); throws the error. – wp-ap Jul 05 '22 at 01:13
  • 1
    Use [strict mode](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode) and don't use implicit global variables. You'll write better code as a consequence. TL;DR put `let timer;` above this code – Phil Jul 05 '22 at 01:14
  • @Phil Thank you. I added let timer; and no more errors. – wp-ap Jul 05 '22 at 01:19

0 Answers0