Jump to content

Need a delay :/ (Java)

WhatComesAround

Hey everyone, I don't know java. I would learn it but I am learning another language and I would rather not mix and match, I learned from that mistake in highschool. Anyways I have a blog and I would like to add a script that would redirect users to a random page each time they visit. I found this online:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
(function(n){
 var pages = ['page1.htm', 'page2.htm', 'page3.htm'];
 n = n < 3? 0 : n < 8? 1 : 2;
 window.location.replace(pages[n]);
})(Math.floor(Math.random() * 10));
</script>
</head>
<body>

</body>
</html>

 

And it works but I get redirected to different pages every second. I was wondering if any of you could help me fix this. Thanks in advance! :D

Link to comment
Share on other sites

Link to post
Share on other sites

Firstly, not sure would would do this I would hate it as a user. Secondly have a look here http://www.w3schools.com/js/js_timing.asp

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, vorticalbox said:

Firstly, not sure would would do this I would hate it as a user. Secondly have a look here http://www.w3schools.com/js/js_timing.asp

Plus both codes don't offer what I am looking for. I am looking for a delay after a redirection not before the redirection. Either I can do this or maybe someone could point out the flaw in the code above that is making this wierd loop. I don't do java and it would take some time for me to get it without learning the language.

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, WhatComesAround said:

Hey everyone, I don't know java

JavaScript and Java are different languages, which one are you talking about?

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, WhatComesAround said:

Plus both codes don't offer what I am looking for. I am looking for a delay after a redirection not before the redirection. Either I can do this or maybe someone could point out the flaw in the code above that is making this wierd loop. I don't do java and it would take some time for me to get it without learning the language.

set time out loads the function the waits for the time then starts, your first redirect will of course wait 3 seconds then change, which will trigger and wait 3 seconds then change.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, WhatComesAround said:

I don't know java.

The code that you have listed is not Java...

4 hours ago, WhatComesAround said:

I would learn it but I am learning another language and I would rather not mix and match, I learned from that mistake in highschool.

That is an absolutely ridiculous excuse.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

18 hours ago, WhatComesAround said:

Hey everyone, I don't know java. I would learn it but I am learning another language and I would rather not mix and match, I learned from that mistake in highschool. Anyways I have a blog and I would like to add a script that would redirect users to a random page each time they visit. I found this online:

 


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
(function(n){
 var pages = ['page1.htm', 'page2.htm', 'page3.htm'];
 n = n < 3? 0 : n < 8? 1 : 2;
 window.location.replace(pages[n]);
})(Math.floor(Math.random() * 10));
</script>
</head>
<body>

</body>
</html>

 

And it works but I get redirected to different pages every second. I was wondering if any of you could help me fix this. Thanks in advance! :D

Assuming you meant JavaScript and HTML5, not Java (2 completely different things as others have said), I have a few pointers to give:

 

1. Replace that 1st line to <!DOCTYPE html>, You need to be in the HTML5 party, not the obsolete HTML4 with the long DOCTYPE.

2. Put the script to the bottom of the <body> instead of placing it inside <head>. that way all of your elements loads before the script executes. Example:

<!DOCTYPE html>
<head>
  <title>Woot a Title!!!</title>
  
  <style>
    p {
      color: #00F;
    }
  </style>
</head>

<body>
  <p>Yeah I am blue!</p>
  
  <script>
    (function() {
      alert('Yeah I loaded all of the elements!');
    })();
  </script>
</body>

 

3. Let me fix that script up for you:

<script type="text/javascript">
  (function() {
    const DELAY = 3000; // In milliseconds

    var urls = ['url1', 'url2', 'url3']; // Stick as many URL's here as you want
    var multiplier = 10 * (urls.length / (urls.length % 10)); // So you can have as many URL's as you want.
    
    // select a random page
    var num = Math.floor(Math.random() * multiplier) % urls.length;
    var selection = urls[num];
    
    // replace the page after DELAY
    setTimeout(function() {
      location.replace(selection);
    }, DELAY);
  })();
</script>

 

 

4. Really? You want pages to redirect to each other once in every whatever milliseconds? Talk about wasting bandwidth and data. I can provide another solution to that if you like.

That one smart guy.

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, Brekmister said:

Assuming you meant JavaScript and HTML5, not Java (2 completely different things as others have said), I have a few pointers to give:

 

1. Replace that 1st line to <!DOCTYPE html>, You need to be in the HTML5 party, not the obsolete HTML4 with the long DOCTYPE.

2. Put the script to the bottom of the <body> instead of placing it inside <head>. that way all of your elements loads before the script executes. Example:


<!DOCTYPE html>
<head>
  <title>Woot a Title!!!</title>
  
  <style>
    p {
      color: #00F;
    }
  </style>
</head>

<body>
  <p>Yeah I am blue!</p>
  
  <script>
    (function() {
      alert('Yeah I loaded all of the elements!');
    })();
  </script>
</body>

 

3. Let me fix that script up for you:


<script type="text/javascript">
  (function() {
    const DELAY = 3000; // In milliseconds

    var urls = ['url1', 'url2', 'url3']; // Stick as many URL's here as you want
    var multiplier = 10 * (urls.length / (urls.length % 10)); // So you can have as many URL's as you want.
    
    // select a random page
    var num = Math.floor(Math.random() * multiplier) % urls.length;
    var selection = urls[num];
    
    // replace the page after DELAY
    setTimeout(function() {
      location.replace(selection);
    }, DELAY);
  })();
</script>

 

 

4. Really? You want pages to redirect to each other once in every whatever milliseconds? Talk about wasting bandwidth and data. I can provide another solution to that if you like.

another solution being iframes right? Right.....

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

6 hours ago, vorticalbox said:

another solution being iframes right? Right.....

I was thinking of using div elements with CSS3 and have a pleasant loading screen so the computer can load the entire site before the user gets to the site (load once, be done with it) instead of having to download an entire page every however much delay.

 

There are good use case scenarios for iframes (Google Maps for example) but, this is not one of them. Using them here would make me cringe.

That one smart guy.

Link to comment
Share on other sites

Link to post
Share on other sites

18 minutes ago, Brekmister said:

I was thinking of using div elements with CSS3 and have a pleasant loading screen so the computer can load the entire site before the user gets to the site (load once, be done with it) instead of having to download an entire page every however much delay.

 

There are good use case scenarios for iframes (Google Maps for example) but, this is not one of them. Using them here would make me cringe.

 

I was joking lol this is how I would do it too have all the content there but hidden then use JS to change what is visable. 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

On 12/3/2016 at 7:38 PM, Nuluvius said:

The code that you have listed is not Java...

That is an absolutely ridiculous excuse.

Yea I should learn it :/

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×