Redirect to url when clicking (settimeout)

1

I'm trying to make a function in js so that when the user clicks on a link spend a few seconds and automatically redirect to another url.

function redireccionarPagina() {
  window.location = "http:/#";
}
setTimeout("redireccionarPagina()", 2000);
<a href="hielo-fuego.html" id="texto_mar" onmouseover="mouseOverMar()" onmouseout="mouseOutMar()">
  <img title="Mar" src="css/images/mar_text_white.png" onclick="reload()" />
</a>
<a id="maria">
  <img title="Mar_basic" src="css/images/o_basic.png" />
</a>
<a href="hielo-fuego.html" id="texto_tierra" onmouseover="mouseOverTierra()" onmouseout="mouseOutTierra()">
  <img title="Tierra" src="css/images/tierra_text_white.png" />
</a>
    
asked by Antonio Ángel Estrada Pérez 15.11.2017 в 12:37
source

5 answers

2

If you want any of your links to be redirected to a url, I recommend assigning all your links an event so that when you click on each one of them, you will be redirected to the url in question.

To do this, you can assign the function addEventListener to each of your links, using the function setTimeout within it to wait 2 seconds since you click until the page is displayed.

Example:

var enlaces = document.getElementsByTagName("a");

for (var i = 0; i < enlaces.length; i++) {
    enlaces[i].addEventListener("click", function(){
      setTimeout(function(){
          window.open("https://www.google.es")
      }, 2000);
    });
}
<a href="#">Esto es un link</a>
<a href="#">Esto es otro link</a>
<a href="#">Esto es otro link</a>
    
answered by 15.11.2017 в 13:08
1

You have to detect the click, cancel the default behavior, and then do the setTimeout:

let a=document.getElementById('enlace');
a.addEventListener('click',function (event) {
  event.preventDefault(); //esto cancela el comportamiento del click
  setTimeout(()=> location.href="https://www.google.com",1000);
});
<a href="https://www.google.com" id="enlace">Ir a google.com en un segundo</a>

The example will not be executed because StackOverflow does not allow it in the code of the answers, but it should work perfectly.

    
answered by 15.11.2017 в 13:07
1

Use setTimeout to wait for the time you need and call the function that opens the URL.

function redireccionarPagina(){
    window.setTimeout( abrirURL, 3000 ); // 3 segundos
};
    
function abrirURL(){
    //Abrir URL que necesites
    console.log("Han pasado 3 segundos");
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="javascript:void(0)" onclick="redireccionarPagina()">Abrir</a>
    
answered by 15.11.2017 в 12:56
1

With JQuery it would be like this

$(document).on("click","#toGoogle",function(){
  setTimeout(function(){ window.location = "https://www.google.com.mx"; }, 1000);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a id="toGoogle">Link<a/>
    
answered by 15.11.2017 в 17:02
0

function mouseOverTierra_movil() {
    document.getElementById("texto_mar_movil").innerHTML = '<img src="css/images/mar_text_nowhite.png" />';
    document.getElementById("texto_tierra_movil").innerHTML = '<img src="css/images/tierra_text_white.png" />';
    document.getElementById("maria_movil").innerHTML = '<img style="max-width:85%;" src="css/images/tierra_img.png" />';
    setTimeout(()=> location.href="http://paginaprueba.es/mariadelao/hielo-fuego.html",2200);
}
<a href="#" id="texto_mar_movil" onmouseover="mouseOverMar_movil()"><img title="Mar" src="css/images/mar_text_white.png"/></a>
    
answered by 15.11.2017 в 17:09