Well, I've got this working in the most basic way possible (thanks to the help given here!)
Currently it just goes through my array and displays a new comment every 5 seconds.
Next, I'll start adding speed up, slow down and jump-to-end buttons.
However, I'd also like to know if anyone can give me advice on how to improve what I've got here. It feels like a hack, and I'd like something a bit more robust and modern feeling.
Here's my code:
<html>
<head>
<script type="text/javascript">
var commentsArray = new Array();
var delay = 5000;
var arrayPos = 0;
<?php
foreach($commentaryOutput as $sentence)
echo "commentsArray[commentsArray.length] = '" . addslashes($sentence) . "';\n";
?>
var arraySize = commentsArray.length;
function timedOutput() {
if (arrayPos < arraySize) {
document.getElementById("currentRow").getElementsByTagName('td')[0].innerHTML = commentsArray[arrayPos];
<!--if (arrayPos > 0)
document.getElementById("previousRow").getElementsByTagName('td')[0].innerHTML = commentsArray[arrayPos - 1];
-->
}
arrayPos++;
setTimeout("timedOutput()", delay);
}
</script>
</head>
<body>
<table id="resultsTable">
<tr id="previousRow"><td> </td></tr>
<tr id="currentRow"><td> </td></tr>
</table>
<script type="text/javascript">timedOutput();</script>
</body>
</html>
Hopefully manipulating the Javascript "delay" variable shouldn't be too hard, although I guess that as soon as, say, someone clicks speed up, the currently running timeout should be stopped so that I don't get in a kerfuffle! 🙂
Thanks in advance for any help making this code less...well...newbie-ish! 🙂
Edit: In an ideal situation, I'd like to start with one row of output, and generate four more, one at a time...so that once there have been 5 or more comments, you can see a "history" of the current comment and the previous 4. But that's waaaay beyond me right now!