-2

I am currently using the following code to redirect a visitor after a set time.

(function(){
    setTimeout(function(){
        window.location="http://google.com/";
    },3000); /* 1000 = 1 second*/
})();

How would I make this a random time from 10-25 seconds?

Nick Zuber
  • 5,467
  • 3
  • 24
  • 48
terry
  • 1
  • try to use javascript function Math.random() – MNF Apr 07 '16 at 22:23
  • Just a friendly tip, you may want to read over this page: [The How-To-Ask Guide](https://stackoverflow.com/help/how-to-ask) so you can always be sure that your questions are easily answerable and as clear as possible. Be sure to include any efforts you've made to fix the problem you're having, and what happened when you attempted those fixes. Also don't forget to your show code and any error messages! – Matt C Apr 07 '16 at 22:28

1 Answers1

1

You just need to create a variable that will randomly select a number between 10-25, then use that value as the interval (don't forget to multiply by 1000 to convert milliseconds to seconds)

(function(){
    var maxTime = 25;
    var minTime = 10;
    var randomTime = Math.floor(Math.random() * (maxTime - minTime + 1)) + minTime;

    setTimeout(function(){
        window.location="http://google.com/";
    }, randomTime * 1000);
})();
Nick Zuber
  • 5,467
  • 3
  • 24
  • 48