Javascript Setinterval not working... where did I go wrong?

Asked

Viewed 45 times

1

As you can see in my code below, I needed to turn my image into Javascript link instead of using the simple HTML method (for specific reasons), but I need it to take a 3-second interval for the boot function, but I couldn’t... you can tell me what I did wrong?

<a name="subir">aqui</a>
<div style="height: 1000px; width: 100%;"></div>
<img src="next.png" id="cima">

<script type="text/javascript">
 document.getElementById('cima').addEventListener('click', function() {
 location.href = '#subir'}, 3000);
</script>
  • You actually only used the wrong function. First see the documentation of addeventlistener and then check if this helps you https://answall.com/q/23861/57801

  • You’re right, wrong syntax! as said @dvd

1 answer

1


Wrong syntax. Do not use setInterval (will run the code endlessly), use setTimeout (runs only 1 time):

document.getElementById('cima').addEventListener('click', function() {
   setTimeout("location.href = '#subir'", 3000);
});
<a name="subir">aqui</a>
<div style="height: 1000px; width: 100%;"></div>
<img src="next.png" id="cima">

  • Not because setInterval is continuous, while setTimeout only runs 1 time.

  • Wow, exactly! Thank you. I’ll be back to select as the correct answer :)

  • @Everson Obg Everson.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.