简体   繁体   English

为什么HTML代码中的settimeout功能不起作用?

[英]Why isn't my settimeout fuction in HTML code working?

I want to have my code show a video and then default to a different code after a specified time using setTimeout. 我想让我的代码显示一个视频,然后使用setTimeout在指定时间后默认使用不同的代码。 Below is my code. 以下是我的代码。 The video file shows up but after the specified 1 second, it disappears without defaulting/showing the second code. 视频文件显示但在指定的1秒后,它会消失而不会默认/显示第二个代码。 It just ends up being blank. 它最终变成了空白。

<div id="video159795815158585" style="width: 300px; height: 250px;">
<script src="http://p.algovid.com/player/tlvplayer.js?p=1597958151&sid=[REPLACE TO SPECIFIC DOMAIN]&cb=58585&w=300&h=250&d=[REPLACE TO SPECIFIC DOMAIN]" type="text/javascript">
</script>
<script type="text/javascript">
setTimeout(function(){ document.getElementById('video159795815158585').innerHTML = "<script type="text\/javascript" src="\/\/display.blutonic-ads.com\/2.0\/9608\/adtag.js" data-tc-slot="26539" data-tc-size="300x250" data-tc-publisher="41b30f06-306c-5b38-9b50-7a5bdd3d9f9a"><\/script> <noscript> <img src="http:\/\/p.algovid.com\/ppx\/error?en=1&em=nojs&p=1597958151&sid=[REPLACE INTO DOMAIN]&cb=58585&domain=[REPLACE TO SPECIFIC DOMAIN]" width="1" height="1"><\/noscript>"; }, 1000);
</script>
<noscript>
    <img src="http://p.algovid.com/ppx/error?en=1&em=nojs&p=1597958151&sid=[REPLACE INTO DOMAIN]&cb=58585&domain=[REPLACE TO SPECIFIC DOMAIN]" width="1" height="1">
</noscript>

This is the second code that should be showing up 这是应该出现的第二个代码

<script type="text/javascript" src="//display.blutonic-ads.com/2.0/9608/adtag.js" data-tc-slot="26539" data-tc-size="300x250" data-tc-publisher="41b30f06-306c-5b38-9b50-7a5bdd3d9f9a"></script>

Obviously syntax error about quotation in your JavaScript code. 很明显,您的JavaScript代码中的引用语法错误。 when the string is referenced and the string is JavaScript statement, you need nested quotations by single versus double quotation or escape character '\\'. 当引用字符串并且字符串是JavaScript语句时,您需要通过单引号与双引号或转义字符'\\'进行嵌套引用。

The following three expressions are equivalent and correct. 以下三个表达式是等效和正确的。

"<script type='...'>"

'<script type="...">'

"<script type=\"..\">"

Please, be careful about your using of single versus double quotation marks. 请注意使用单引号和双引号。 the effects of single and double quotation are same in JavaScript, you can mix single and double quotation marks when creating a string, as long as the string uses the same type of quotation mark at the beginning and end. 单引号和双引号的效果在JavaScript中是相同的,只要字符串在开头和结尾使用相同类型的引号,就可以在创建字符串时混合使用单引号和双引号。


The above description only solved the syntax error, but did not solve the function problem. 以上描述仅解决了语法错误,但没有解决功能问题。 the function problem you faced is how to dynamically load a JavaScript script file, The following code wants to help you. 您遇到的功能问题是如何动态加载JavaScript脚本文件,以下代码希望对您有所帮助。

<script type="text/javascript">
    var ohead= document.getElementsByTagName('head')[0];
    var oScript= document.createElement("script");
    oScript.type = "text/javascript";
    oScript.src= "//display.blutonic-ads.com/2.0/9607/adtag.js";
    ...
    setTimeout("oHead.appendChild(oScript)",1000);
</script>

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

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