Jump to content

Recommended Posts

Hi, is it possible to change a text string after a 5 second period? What i want is text string "A" to display on page, then after five seconds replace text string "A" with text string "B" then after another 5 seconds replace with text string "C" then after 5 seconds display text string "A" again and keep doing this?

Link to comment
https://forums.phpfreaks.com/topic/245528-change-text-string-after-seconds/
Share on other sites

[Moved to JavaScript forum]

 

You will need to accomplish this using JavaScript. How many strings do you have? If you only have a relatively small number then just create the text strings as JavaScript values (in an array). If it is an excessively large number then you would use AJAX (JavaScript + PHP, or some other server-side technology) to get them from the server.

Ok, I am working on that. I have 3 strings. But some code I found in a tutorial is making me click a button to activate the timer and I don't want a timer so I am still looking into it. By the way thanks for moving my post to the proper section. :)

You will want to create a function that is run "onload" of the page. That function should get the next line of text to display and populate a div with that content (hint, "innerHTML"). Then at the end of the function you would use settimeout() to call the function again (i.e. make it recursive) after 5 seconds.

ok so far I have this which does display my messages (3 of them) using

 

<script type="text/javascript">
var customMessages = Array('Click \"Image\" to see server details....', 'Click \"Map Name\" to go directly to download link for that map....', 'Click \"Player Scores\" to see top 10 players for that server');

var secondsPassed = 0;
function show_notes()
{
var t = setTimeout("load()", 3000);
}

function load()
{
document.getElementById("tracker_notes").style.color = "red";
document.getElementById("tracker_notes").innerHTML = customMessages[secondsPassed];
if(++secondsPassed < 6)
	var t = setTimeout("load()", 4000);
else
	document.getElementById("tracker_notes").innerHTML = '';
}
</script>

 

however at the end of the third code it then displays "undefined as if it were another message. I can't for the life of me see what is undefined.

 

I am also working on getting this function to keep reloading so that it repeats the 3 messages over and over. Am I going in the right direction or am I way off here?

You are in the right direction. You are getting undefined because your array has 3 values and you are trying to get 6 values. Change your if statment as the following

// change this
if(++secondsPassed < 6)

// to this
if(++secondsPassed < customMessages.length)

Thank you but after getting this code incorporated into the actual page it is going to be used on I realized that I don't need the loop feature as the page refreshes itself at 2 minute intervals. So having 18 values in the array it works out good. Thank you both for your help.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.