我有一个脚本,允许我添加一系列的图像和链接,本质上是一个广告旋转器。运行后,它设置一个超时,以便定期调用脚本的旋转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);
};
});这在第一次运行时很好,但是随后对'ad_rotate‘函数的调用会导致错误。萤火虫给:
ReferenceError: ad_rotate is not defined
setTimeout("ad_rotate("+active+")", 5000);我有什么办法纠正这件事吗?
发布于 2014-03-30 11:21:54
这样做:
setTimeout(function () {
ad_rotate(active);
}, 5000);不能在现在的settimeout函数中传递参数。您需要在它周围包装一个匿名函数。
发布于 2014-03-30 11:23:06
这应能解决以下问题:
setTimeout(ad_rotate(active), 5000);https://stackoverflow.com/questions/22743355
复制相似问题