JavaScript SetInterval not working ... where did I go wrong? [duplicate]

1

As you can see in my code below, I needed to make my image link via JavaScript instead of using the simple HTML method (for specific reasons), however I need it to take a 3-second interval for the boot function, but I could not ... can you tell me what I was wrong about?

<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>
    
asked by anonymous 27.02.2018 / 17:40

1 answer

1

Wrong syntax. Do not use setInterval (will run the code endless times), use setTimeout (execute only once):

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">
    
27.02.2018 / 17:45