と呼ばれる関数があります
BeforeSlide1() {
...
}
BeforeSlide2() {
...
}
そして、これらへの呼び出しをループで動的に作成できるようにしたいと思います。次のようなものです。
code = "BeforeSlide" + slide + "()"
eval(code);
今、私のこのコードは明らかに機能しません(それがそれほど単純だったとしても!)誰か知っていますか?
よし、全体像を描くぞ。
簡単なスクリプトでスライドショーを実行しています
function slideshow(slide) {
if (pauseTimes[slide]>0) {
/* if there is another slide after the one currently showing... */
//Call BeforeSlide-function if there is one
code = "BeforeSlide" + slide + "();"
try {
eval(code);
} catch(e) {
/* do nothing since many slides don't have one */
}
$('#slide'+slide).
fadeIn(1500,function(){}).
delay(pauseTimes[slide]).
fadeOut(1500,function(){slideshow(slide+1);});
;
}
else {
/* if there is no more slide and we've reached the end, try to ajax-refresh the slideshow contents */
ajaxUpdate();
}
}
スライド自体の HTML の中で、before-and after 関数は次のように定義されています。
/* fire the slideshow from slide 1 once document is finished loading */
$(document).ready(function(){
slideshow(1);
});
</script>
</HEAD>
<body id="slideshow_body">
<script id='pauseTimesScript' type='text/javascript'>
/* Define pauseTimes (array holding duration of each slide) */
try {
pauseTimes=null;
pauseTimes=undefined;
pauseTimes = new Array();
setPauseTimes();
/*alert('satte först till null, deklarerade sedan om.');*/
} catch(e) {
pauseTimes = new Array();
setPauseTimes();
/*alert('deklarerade utan att först sätta null.');*/
}
function setPauseTimes() {
pauseTimes[1]=2000;
pauseTimes[2]=2000;
pauseTimes[3]=2000;
}
</script>
<div id="avbrott">Tillfälligt avbrott, visar cachelagrat innehåll sedan 2012-10-31 16:37:35</div>
<div id="canvas" style="width:1072px;height:732px;">
<script language='text/javascript'>
function BeforeSlide1(order=1) {
alert('here goes slide '+order);
}
</script>
<div id="slide1" class="slide" style="z-index:1;">
<div id="slide_bg" style="float:left;height:100%;width:100%;background-color:#ffffff;background-image:url('http://bglabs.evade.netdna-cdn.com/45875kli90/505.jpg');">
<div style="background-color:#eeeeee;color:#000000;float:right;text-align:center;margin-top:30px;padding:10px;font-weight:bold;font-size:30pt;" id="preview_title">Egypt rocks!</div>
<div style="clear:both;float:none;"></div>
<p style="color:#000000;margin:10px 10px 0 20px;font-style:italic;font-size:38pt;" id="preview_data_6">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse feugiat pharetra eros, vitae ullamcorper tellus condimentum sit amet. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Suspendisse in leo erat. Aliquam interdum molestie tortor, nec lacinia mauris viverra eget. Suspendisse aliquam tempor scelerisque. Proin vel purus nunc. Pellentesque ut sapien libero.<br />
</p>
</div>
この BeforeSlide 関数の目的はもちろん、アラートを生成する以外のことを行うことです...
より良い方法で構造化する方法を教えてください。=)