[TYPO3] levelmedia: -1

Georg Rehfeld georg.rehfeld at gmx.de
Fri Jun 29 02:19:18 CEST 2007


Hi Bas, all,

Bas van der Togt wrote:
> Another question. Now all the time the slideshow is starting with the 
> first image. Is it possible to rotate this with TS?

I see no simple way to do that with TS, but you copy over the image
names already from one JS array to another. So my approach would be
to modify the copy process in JS. For a simple rotation you could
calc a random offset and use that in the copy process like so
(untested!):


   var tmpArray = tempString.split(",");
   var imgArray = new Array();
+ var offset = Math.floor(Math.random() * tmpArray.length);
   for (var ii = 0; ii < tmpArray.length; ii++) {
-     imgArray[ii] = tmpArray[ii];
+     var kk = ii + offset;
+     if (kk >= tmpArray.length) {
+         kk -= tmpArray.length;
+     }
+     imgArray[ii] = tmpArray[kk];
   }
   myShow = new Slideshow(.....


For a total random display you might use array.splice() to cut out
random elements from the source array and insert them in order into
the target array like so (untested too!):


   var tmpArray = tempString.split(",");
   var imgArray = new Array();
- for (var ii = 0; ii < tmpArray.length; ii++) {
-     imgArray[ii] = tmpArray[ii];
- }
+ var offset;
+ while (tmpArray.length > 0) {
+     offset = Math.floor(Math.random() * tmpArray.length);
+     // push the random source element to the end of the target array
+     // (neccessary for compatibility with different JS versions)
+     imgArray.push(tmpArray[offset]);
+     // drop the source element (shortens the source array)
+     tmpArray.splice(offset, 1);
+ }
   myShow = new Slideshow(.....


There might be even better ways to do similar, e.g. array.sort() with
a sort function, that dosn't look at the values to compare, but uses
Math.random() instead (however, would that ever terminate the sort?)
or maybe array.map().

This is just what came up to my mind (thanks to my PERL background ...
splice() was invented there originally AFAIK and other languages
derive from PERL a lot).

But this isn't a JavaScript/PERL list, is it?

HTH, Georg
-- 
   ___   ___
  | + | |__    Georg Rehfeld      Woltmanstr. 12     20097 Hamburg
  |_|_\ |___   georg.rehfeld.nospam at gmx.de    +49 (40) 23 53 27 10

               (Delete .nospam from mail address)


More information about the TYPO3-english mailing list