简体   繁体   English

javascript全局变量不适用于外部js文件

[英]javascript global variable not working with external js file

I have declared the following variable outside of an external js file because part of it is generated server side. 我已经在外部js文件外部声明了以下变量,因为它的一部分是在服务器端生成的。

<script type="text/javascript">
     var images=new Array(<%= Master.slideshowArray %>);
</script>

For some reason, by removing this from the external js file (below) the slideshow no longer works... I am guessing I've made an error declaring it as a global variable or perhaps there's something else I need to declare globally... Any ideas? 出于某种原因,通过从外部js文件中删除此文件(如下),幻灯片将不再起作用...我猜我在将其声明为全局变量时出错,或者可能需要在全局中声明其他内容。 。 有任何想法吗?

var nextimage=0;

doSlideshow();

function doSlideshow()
{
    if($('.backgroundImage').length!=0)
    {
        $('.backgroundImage').fadeOut(500,function(){slideshowFadeIn();$(this).remove();});
    }
    else
    {
        slideshowFadeIn();
    }
}

function slideshowFadeIn()
{
    if(nextimage>=images.length) 
        nextimage=0;

    $('.homeLeadContent').prepend($('<img class="backgroundImage" src="'+images[nextimage++]+'" style="display:none;">').fadeIn(500,function() {
        setTimeout(doSlideshow,1000);
    }));
}

Does the script tag for the external js file come before the var images=... inline script tag? 外部js文件的脚本标签是否位于var images=...内联脚本标签之前?

Browsers execute code in the order that they see them, so if the external js file is seen first, it'll execute the doSlideShow() function which may call the slideshowFadeIn() which would try and references a non-existent-at-that-point images variable. 浏览器按照看到它们的顺序执行代码,因此,如果首先看到外部js文件,它将执行doSlideShow()函数,该函数可能会调用slideshowFadeIn(),该函数将尝试引用不存在的那个点图像变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM