首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未定义Ad Rotator脚本函数

未定义Ad Rotator脚本函数
EN

Stack Overflow用户
提问于 2014-03-30 11:20:43
回答 2查看 208关注 0票数 1

我有一个脚本,允许我添加一系列的图像和链接,本质上是一个广告旋转器。运行后,它设置一个超时,以便定期调用脚本的旋转opart,以更改图像和链接。

代码语言:javascript
复制
$(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‘函数的调用会导致错误。萤火虫给:

代码语言:javascript
复制
ReferenceError: ad_rotate is not defined
setTimeout("ad_rotate("+active+")", 5000);

我有什么办法纠正这件事吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-03-30 11:21:54

这样做:

代码语言:javascript
复制
setTimeout(function () {
    ad_rotate(active);
}, 5000);

不能在现在的settimeout函数中传递参数。您需要在它周围包装一个匿名函数。

票数 1
EN

Stack Overflow用户

发布于 2014-03-30 11:23:06

这应能解决以下问题:

代码语言:javascript
复制
setTimeout(ad_rotate(active), 5000);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22743355

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档