[英]javascript global variable not working with external js file
我已经在外部js文件外部声明了以下变量,因为它的一部分是在服务器端生成的。
<script type="text/javascript">
var images=new Array(<%= Master.slideshowArray %>);
</script>
出于某种原因,通过从外部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);
}));
}
外部js文件的脚本标签是否位于var images=...
内联脚本标签之前?
浏览器按照看到它们的顺序执行代码,因此,如果首先看到外部js文件,它将执行doSlideShow()函数,该函数可能会调用slideshowFadeIn(),该函数将尝试引用不存在的那个点图像变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.