frank5050
06-23-2012, 09:08 PM
Hi I am using the following script to display few strings of text with a set delay.
My problem is that after displaying all strings of text, the script goes back to displaying the first string again.
I would like the script to stop after displaying the last text string.
Your help would be appreciated.
<script type="text/javascript">
//addThisEvent -utility function for setting event listeners
//cross-browser, and preserving 'this' within attachEvent.
//el may be a single html element as a DOM object, or the window object,
//type should be string name of event type ('on' prefix not required, so 'mouseover' or 'onmouseover' works fine)
//fn should be a function to set as an event handler
function addThisEvent(el, type, fn) {
var ename = type.replace(/^on/i, '');
if (el.attachEvent) { //IE
el.attachEvent('on'+ ename, function () {
return fn.call(el, window.event); //fixes IE's fudging of 'this' in attachEvent
});
} else if (el.addEventListener) { //Standard
el.addEventListener(ename, fn, false );
} else {
el["on"+ename] = fn;
}
}
addThisEvent(window, 'load', function () {
var i = 0,
frequency = 2000, //rate of change frequency, 1000 = 1 second
el = document.getElementById('rotatingTextParagraph'), //the html DOM element to show the text in
textArray = [
'first text string',
'second text string',
'third text string',
'Fourth text string'
];
el.innerHTML = textArray[i++];
el.textRotationInterval = window.setInterval(function () {
if (i >= textArray.length) {
i = 0;
}
el.innerHTML = textArray[i++];
}, frequency);
});
</script>
My problem is that after displaying all strings of text, the script goes back to displaying the first string again.
I would like the script to stop after displaying the last text string.
Your help would be appreciated.
<script type="text/javascript">
//addThisEvent -utility function for setting event listeners
//cross-browser, and preserving 'this' within attachEvent.
//el may be a single html element as a DOM object, or the window object,
//type should be string name of event type ('on' prefix not required, so 'mouseover' or 'onmouseover' works fine)
//fn should be a function to set as an event handler
function addThisEvent(el, type, fn) {
var ename = type.replace(/^on/i, '');
if (el.attachEvent) { //IE
el.attachEvent('on'+ ename, function () {
return fn.call(el, window.event); //fixes IE's fudging of 'this' in attachEvent
});
} else if (el.addEventListener) { //Standard
el.addEventListener(ename, fn, false );
} else {
el["on"+ename] = fn;
}
}
addThisEvent(window, 'load', function () {
var i = 0,
frequency = 2000, //rate of change frequency, 1000 = 1 second
el = document.getElementById('rotatingTextParagraph'), //the html DOM element to show the text in
textArray = [
'first text string',
'second text string',
'third text string',
'Fourth text string'
];
el.innerHTML = textArray[i++];
el.textRotationInterval = window.setInterval(function () {
if (i >= textArray.length) {
i = 0;
}
el.innerHTML = textArray[i++];
}, frequency);
});
</script>