Javascript定时我的功能

时间:2014-04-12 00:06:05

标签: javascript php html iframe

任何人都可以帮助我解决我在尝试添加到我的定时功能时遇到的错误()。我想要做的是在视频广告展示前延迟10秒。我正在显示的页面被加载到包含页眉页脚等的php系统中。

以下是我目前使用的代码:

<script type="text/javascript" src="http://www.optimalupload.com/upload/download/advertisement.js"></script>
<style type="text/css">
<!--
#tester {
    display:none;
}
--
</style>

<script language="javascript">
var max_time = 10;
var cinterval ;

function countdown_timer(){
  // decrease timer
  max_time--;
  document.getElementById('countdown').innerHTML = max_time;
  if(max_time == 0){
    clearInterval(cinterval);
  }
}
// 1,000 means 1 second.
cinterval = setInterval('countdown_timer()', 1000);
</script>

<div class="contentPageWrapper">
    <div class="pageSectionMainFull ui-corner-all">
        <div class="pageSectionMainInternal">


<script type="text/javascript">
if (document.getElementById("tester") != undefined)
{document.write('<p class="no"> Video beings in <strong><span id="countdown">10</span></strong> seconds. We have detected that you are not using <strong>AdBlock</strong> or some other adblocking software. Your pre-download advertisment video will being shortly.<!-- end .content --></p>');window.setTimeout(function(){
        myFunction();    
  }, 10000);
}
else
{
document.write('<p class="no"> Ads displayed in <strong><span id="countdown">10</span></strong> seconds. We have detected that you are using <strong>AdBlock</strong> or some other adblocking software. You will be using our affliate download page. If you would rather use our default video ad page please refresh with adblock disabled. To download your file simply take a look at our partners below. The download link to your file is embedded into one of the advertisments.<!-- end .content --></p>');window.setTimeout(function(){
    myFunction();        
    }, 10000);
}
</script>

然后我试图定义myFunction();这样:

我是这样做的。

function myFunction() {
<script src="(Link to video ad stream/ Not in directory)" type="text/javascript"></script><noscript><iframe src="(Link to stream with no javascript" scrolling="no" framborder="0" marginheight="0" marginwidth="0" style="width:1px;height:1px;display:none;"></iframe></noscript>
}

然而没有任何作用。倒计时后计时器仍然熄灭但没有任何装载。有没有其他方法来编码这个系统?

3 个答案:

答案 0 :(得分:1)

当计时器到达零时显示广告:

function countdown_timer(){
  // decrease timer
  max_time--;
  document.getElementById('countdown').innerHTML = max_time;
  if(max_time == 0){
    clearInterval(cinterval);
    display_ad();
  }
}

答案 1 :(得分:0)

我相信答案很简单:要为javascript函数的触发器计时,你只需要做这样的事情:

setTimeout("alert('hi!');", 500);

你可以做其他自定义功能而不是那个警报,我只是把它放在那里供你测试。

SetInterval每10秒运行一次,setTimeout只运行一次 - 我相信这就是你需要的吗?

至于你的倒计时,你的方法相当不错,但你应该这样一个setInterval,1秒钟,每次运行时,递减持有秒的变量并更新html,然后当setTimeout运行时(10秒) ,清除间隔 - 或在计数器达到零后自行清除。

答案 2 :(得分:-1)

要动态添加js,请使用类似

的内容
function myFunction(){
    var g = document.createElement('script'), s = document.getElementsByTagName('script')[0];
    g.src = 'yourjsscript.js';
    s.parentNode.insertBefore(g, s);
}

如果禁用了脚本,则无法在脚本标记中生成noscript,因此只需将其作为正常html添加到正常显示视频的位置。

<noscript><iframe src="(Link to stream with no javascript" scrolling="no" framborder="0" marginheight="0" marginwidth="0" style="width:1px;height:1px;display:none;"></iframe></noscript>

所以你需要类似下面这样的东西,因为即使你有一个if(document.getElementById("tester") != undefined)setInterval也会正确执行,所以将它包装在一个函数中并在计时器结束后调用它。 / p>

var max_time = 10;
var cinterval;

function countdown_timer(){
    // decrease timer
    max_time--;
    document.getElementById('countdown').innerHTML = max_time;
    if(max_time == 0){
        clearInterval(cinterval);
        showAds();
    }
}
// 1,000 means 1 second.
cinterval = setInterval('countdown_timer()', 1000);
function showAds(){
    if(document.getElementById("tester") != undefined){
        ...
        window.setTimeout(function(){
            myFunction();    
        }, 10000);
    }else{
        ...
        window.setTimeout(function(){
            myFunction();        
        }, 10000);
    }
}
function myFunction(){
    var g = document.createElement('script'), s = document.getElementsByTagName('script')[0];
    g.src = 'yourjsscript.js';
    s.parentNode.insertBefore(g, s);
}