简体   繁体   中英

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. Below is my code. The video file shows up but after the specified 1 second, it disappears without defaulting/showing the second code. 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. when the string is referenced and the string is JavaScript statement, you need nested quotations by single versus double quotation or escape character '\\'.

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.


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.

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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