sorry about the bad title, but this is a code that takes a string, defined in an array, and converts it to the next string in the array by replacing letters one at a time. it works fine, but i want some modifications.

code:

function scroll(message_loop)
{
var messages = new Array();
messages[0] = "String1";
messages[1] = "String2";
messages[2] = "String3";
if(message_loop >= messages.length)
{
message_loop = 0;
}
var old_value = window.document.getElementById('scroll_text').innerHTML;
var new_value;
var random = Math.floor(Math.random() * messages[message_loop].length);
var change_letter = messages[message_loop].charAt(random);
if(old_value == messages[message_loop])
{
message_loop++;
setTimeout("scroll("+message_loop+")",700);
return;
}
if(change_letter == old_value.charAt(random))
{
scroll(message_loop);
return;
}
var first_part = old_value.substring(0,random);
random++;
var last_part = old_value.substring(random,messages[message_loop].length);
var new_value = first_part + change_letter + last_part;
window.document.getElementById('scroll_text').innerHTML = new_value;
setTimeout("scroll("+message_loop+")",50);
}

i was wondering if there is a way to make it so that each string has its own wait time till the next string starts appearing, so that they dont all go away after the single set time.
also, i was wondering if there is a way to make it so that when it appears inline, it doesn't effect the words around it by pushing them around, because the length is constantly changing.
the last thing is that when one of the strings is just a space, ie stops completely when it reaches that string and doesn't go on. in ff its fine, and goes on to the next one.

thanks for any help, and forgive me for the horrible thread title.
billy

Recommended Answers

All 2 Replies

Billy,

i was wondering if there is a way to make it so that each string has its own wait time till the next string starts appearing, so that they dont all go away after the single set time.

You could do something like this:

var messages = [
	{m:"Hello", d:1000},//m = message; d = delay
	{m:"Goodbye", d:1500},
	{m:"See you soon", d:2000},
	{m:" ", d:750}
];
var old_value = '';
function scroll(message_loop) {
	message_loop = message_loop % messages.length
	var new_value = messages[message_loop].m;
	var random = Math.floor(Math.random() * new_value.length);
	var change_letter = new_value.charAt(random);
	if(old_value == new_value) {
		setTimeout("scroll("+ (message_loop+1) + ")", messages[message_loop].d);
		return;
	}
	if(change_letter == old_value.charAt(random)) {
		scroll(message_loop);
		return;
	}
	var first_part = old_value.substring(0, random);
	var last_part = old_value.substring(random+1, new_value.length);
	old_value = window.document.getElementById('scroll_text').innerHTML = first_part + change_letter + last_part;
	setTimeout("scroll("+message_loop+")",50);
}

Each element in the array is now an object with two properties; m:message and d:delay, which can be extracted as messages.m and messages.d .

Making messages global is not germain, it just gives scroll a little less to do at each iteration.

also, i was wondering if there is a way to make it so that when it appears inline, it doesn't effect the words around it by pushing them around, because the length is constantly changing.

Use a monospaced font.

<style type="text/css">
#scroll_text { font-family:courier; font-size:16pt; }
</style>

the last thing is that when one of the strings is just a space, ie stops completely when it reaches that string and doesn't go on. in ff its fine, and goes on to the next one.

Mmmm, not sure but I cured it by making old_value global and in scroll() setting old_value to the same string that gets displayed, thus obviating the need for var old_value = window.document.getElementById('scroll_text').innerHTML; . I guess that line was doing the damage in IE.

thanks for your help, airshow, it works much better now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.