我有一个脚本,允许我添加一系列图像和链接,基本上是一个广告旋转器。运行后,它会设置一个超时时间,以便定期调用脚本的旋转opart,以便更改图像和链接。
$(document).on("pageshow", function () {-
alert("LOADED BANNER CODE");
//Global variable that stores advertising banners.
var ads = new Array();
//Function that starts when the page finished loading.
//Adds information about the new banners.
ads.push(["http://www.dpbolvw.net/click-7490208-11465196", "http://www.lduhtrp.net/image-7490208-11465196", ""]);
ads.push(["http://www.anrdoezrs.net/click-7490208-11646171", "http://www.ftjcfx.com/image-7490208-11646171", ""]);
/*
ads.push(["", "", ""]);
ads.push(["", "", ""]);
ads.push(["", "", ""]);
ads.push(["", "", ""]);
ads.push(["", "", ""]);
ads.push(["", "", ""]);
ads.push(["", "", ""]);
*/
//ads.push(["http://w3schools.com/svg/default.asp", "http://adn.impactradius.com/display-ad/378-10418", "SVG"]);
//Starting rotation with the first banner.
ad_rotate(0);
function ad_rotate(active){
alert("LOADED BANNER ADROTATE CODE");
//Gets the div that will display banners.
var ad_element = document.getElementById("ad");
//Prints a new link with image in advertising box.
ad_element.innerHTML = "<a href=\""+ads[active][0]+"\"><img src=\""+ads[active][1]+"\" alt=\""+ads[active][2]+"\" title=\""+ads[active][2]+"\" /></a>";
//Switches to the next banner.
active++;
//If the counter has reached the end, it shall start again from zero.
if(active >= ads.length){
active = 0;
}
//Run the function in 5000 milliseconds (5 seconds).
setTimeout("ad_rotate("+active+")", 5000);
};
});
这在第一次运行时效果很好,但随后调用了&#39; ad_rotate&#39;功能导致错误。萤火虫给出:
ReferenceError: ad_rotate is not defined
setTimeout("ad_rotate("+active+")", 5000);
我有什么想法可以纠正这个问题吗?
答案 0 :(得分:1)
这样做:
setTimeout(function () {
ad_rotate(active);
}, 5000);
您不能按原样在settimeout函数中传递参数。你需要在它周围包装一个匿名函数。
答案 1 :(得分:0)
这应该解决它:
setTimeout(ad_rotate(active), 5000);