0

我有调用的函数

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 中,前函数和后函数定义如下:

    /* 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 函数的目的当然是做其他事情而不是产生警报......

请让我知道我应该如何以更好的方式构建它!=)

4

3 回答 3

2

不要使用评估!声明的每个函数都可以通过window. 试试这个:

function test() {
    alert("test");
}

var x = window['test'];
x();

示例小提琴

但是,拥有一个函数并将整数作为参数传递给它会更合乎逻辑。

于 2012-10-31T15:37:26.857 回答
1

我可能会创建一个如下所示的对象,

var myFunc = {
    BeforeSlide1: function () {
        //some stuff
    },
    BeforeSlide2: function () {
        //some stuff
    } 
}

然后你可以简单地打电话myFunc['BeforeSlide' + num]

于 2012-10-31T15:38:42.337 回答
0
var code = "this.f = BeforeSlide" + slide + "() {...}";
eval(code);
于 2012-10-31T15:37:49.280 回答