Loading...
JavaScript | Тайм рамдомный редирект на Javaskript
Всем привет. Есть код
plaintext
<script type="text/JavaScript">
      setTimeout("location.href = '//google.com';",8000);
 </script>
:
Который перенаправляет на определенный адрес через 8 секунд.
plaintext
<script type="text/javascript">
  var rand = Math.floor(Math.random()*2)
  if (rand == 1){
    window.location = "http://www.google.com/"
} else {
  window.location = "http://www.yandex.ru/"
  }
</script>
Этот код рамдомно моментально перенаправляет на один из адресов.

У меня вопрос, как можно их объеденить? Мне нужно что бы был рамдомный редирект на разные адреса но только спустя 8 секунд. Подскажите пожалуйста )
________
посл. ред. 06.04.2020 в 20:14; всего 3 раз(а); by KORUP
KORUP, как то так) проверил, вроде работает
javascript
<script type="text/javascript">
  var rand = Math.floor(Math.random()*2)
  if (rand == 1){
    setTimeout("window.location = '//google.com';",8000);
} else {
  setTimeout("window.location = '//www.yandex.ru';",8000);
  }
</script>

________
посл. ред. 06.04.2020 в 20:28; всего 1 раз(а); by Архимед
Архимед (06.04.2020 в 20:28)
KORUP, как то так) проверил, вроде работает
javascript
<script type="text/javascript">
  var rand = Math.floor(Math.random()*2)
  if (rand == 1){
    setTimeout("window.location = '//google.com';",8000);
} else {
  setTimeout("window.location = '//www.yandex.ru';",8000);
  }
</script>
О, круто, а не подскажешь как добавить еще линков?
KORUP,
javascript
<script type="text/javascript">
  var rand = Math.floor(Math.random()*5)
if (rand == 1){
    setTimeout("location.href = '//google.com';",8000);
} else if (rand == 2) {
  setTimeout("location.href = '//www.yandex.ru';",8000);
  } else if (rand == 3) {
setTimeout("location.href = '//bym.guru';",8000);
} else if (rand == 4) {
setTimeout("location.href = '//liveinternet.ru';",8000);
}
</script>

________
посл. ред. 06.04.2020 в 20:50; всего 1 раз(а); by Архимед
KORUP,
javascript

function getRandomInt(max) {
  return Math.floor(Math.random() * Math.floor(max));
}
// Вместо max вставляешь нужное число для линков
switch(getRandomInt(3)) {
  case '0': 
    setTimeout("window.location = '//www.yandex.ru';",8000);
  break;
  case '1':
    setTimeout("window.location = '//www.yandex.ru';",8000);
  break;
  case '2':
    setTimeout("window.location = '//www.yandex.ru';",8000);
  break;

  default: // Если никакое из доступных выше чисел
    alert('ERROR');
  break;
}
Архимед (06.04.2020 в 20:49)
KORUP,
javascript
<script type="text/javascript">
  var rand = Math.floor(Math.random()*5)
if (rand == 1){
    setTimeout("location.href = '//google.com';",8000);
} else if (rand == 2) {
  setTimeout("location.href = '//www.yandex.ru';",8000);
  } else if (rand == 3) {
setTimeout("location.href = '//bym.guru';",8000);
} else if (rand == 4) {
setTimeout("location.href = '//liveinternet.ru';",8000);
}
</script>
Не работает. Срабатывает второй и четвертый линк только.
Вот так будет правильней.

<script type="text/javascript">
function redirect() {

// Массив со ссылками
var links = ['http://link1.ru', 'http://link2.ru','http://link3.ru', 'http://link4.ru'];

// Получаем рандомный элемент...
var rand = Math.floor(Math.random() * links.length);

// ...и перенаправляем
window.location = links[rand];
}

setTimeout(redirect, 8000);
</script>
Всем спасибо что попытались )
Онлайн: 0
Время:
Gen. 0.0933
(c) Bym.Guru 2010-2026