Skip to content Skip to sidebar Skip to footer

Problems With Javascript Settimeout Function

I have a problem that i want to bind my setTimeout $(document).click(function () { function aktivereSkift() { $(this).attr('src', '/lib/pictures/picA.png'); set

Solution 1:

First of all, you should not pass a string to setTimeout: this is bad practice. Pass a function, and make it an arrow function so that you can still refer to the same this as in the statement before it:

functionaktivereSkift() {
      $(this).attr("src", "/lib/pictures/picA.png")
      setTimeout(() => $(this).attr('src', '/lib/pictures/picB.png'), 3000);
  }

NB: it is strange that you put your code in a $(document).click handler.

Post a Comment for "Problems With Javascript Settimeout Function"