简体   繁体   English

javascript工作在本地主机,但不能托管?

[英]javascript working in localhost but not when hosted?

I'm trying to implement a simple image slider on my website but it doesn't seem to want to work. 我正在尝试在我的网站上实现一个简单的图像滑块,但它似乎不起作用。 Nothing happens at all. 什么都没发生。 Nothing I can think of has worked thus far so I thought I'd ask here. 到目前为止,我能想到的一切都没有奏效,所以我想在这里问。 This is my code. 这是我的代码。

window.onload = function(){
   image = document.getElementById("slideShow");
   index = 0;

   setInterval(slide, 3000);
};

function slide(){
    index++;
    if(index >= 6) {index = 0;}
    path = "images/Slideshow/image"+index+".jpeg";

    image.setAttribute("src",path);
}

window.onload is pretty cheesy because nobody seems to agree on what onload actually means. window.onload很俗气,因为似乎没有人同意onload的实际含义。

i would recommend you use 我建议您使用

document.addEventListener("DOMContentLoaded", function(){
    // code
});

Another thing u should be aware of is slow loading. 您应该注意的另一件事是加载缓慢。 If the scripts gets loaded after the ready event has been fired then your code will never trigger. 如果在ready事件触发后加载脚本,则您的代码将永远不会触发。

A solution is wrapping the ready function is a seperate module. 包装就绪功能的解决方案是单独的模块。

var eventReady;
(function(){
    var funcArr = [];
    eventReady = function(func){
        funcArr.push(func);
    }
    document.addEventListener("DOMContentLoaded", function(){
        for(var i=0;i<funcArr.length;i++){
            try{
                funcArr[i]();
            }
            catch(e){
                throw(e);
            }
            finally{
                continue;
            }
        };
        eventReady = function(func){
            func();
        }
    });
})();

and then use the module like: 然后像这样使用模块:

eventReady(function(){
    // code
})

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

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