Cannot find the exact syntax of setTimeout with innerHTML as argument.
I am developing a very simple function that should allow me to display a countdown message:
var myTable=document.getElementById("myTable");
var myHTML0 = myTable.outerHTML;
var j=0;
var T=5;
for (g = 0; g<T; g++) {
j = T-g;
myMsg = "We have "+j+" seconds missing to go.";
myHTML = myHTML0.substring(0,myHTML0.indexOf("We")) +myMsg+myHTML0.substring(myHTML0.indexOf("We")+2,myHTML0.length);
*** var k = setTimeout("document.getElementById('myNote').innerHTML = myHTML", g*1000);
myHTML ="";
}
Unfortunately, I canno manage to spot the correct syntax of line ***. Can you suggest how? Thanx.
JavaScript
Last Comment
Sar1973
8/22/2022 - Mon
leakim971
try this ;
var myTable=document.getElementById("myTable");var myHTML0 = myTable.outerHTML;var j=0;var T=5;for (g = 0; g<T; g++) { j = T-g; myMsg = "We have "+j+" seconds missing to go."; myHTML = myHTML0.substring(0,myHTML0.indexOf("We")) +myMsg+myHTML0.substring(myHTML0.indexOf("We")+2,myHTML0.length);*** var k = setTimeout("document.getElementById('myNote').innerHTML = '" + myHTML + "'", g*1000); myHTML ="";}
I have tried these:
1. (posted) setTimeout("document.getElementById('myNote').innerHTML = myHTML", g*1000);
2. setTimeout("document.getElementById('myNote').innerHTML = '" + myHTML + "'", g*1000);
3. setTimeout("document.getElementById('myNote').innerHTML = "+ myHTML, g*1000);
4. setTimeout("document.getElementById('myNote').innerHTML = \'myHTML\', g*1000); (or something similar).
In case 2. it says that I've inserted a string without termination, in the other cases the editor alerts of syntax errors (3.) or returns a null value (1.), so that the table gets empty; moreover it does not support jQuery.
It's not published yet. But I'm not sure that I need to make the string portable with escape function; I should understand instead why I get "string without termination" error even if I point the HTML code with single marks.
leakim971
about your "string without termination" error :
var blabla = "some string without error";
var bloblo = "string with-----> " <------- termination error";
Sorry, you are asking which is the exact error message I get?
leakim971
No, I want you understand why you get the error.
bloblo have a double quote in the string, it need to be escaped
Sar1973
ASKER
I can also tell you that without using quotes I get a blank content/value in the table in subject, while it works if I use the absolute syntax without timing "document.getElementById('myNote').innerHTML = myHTML;".
You said : << "escape" does not work at all (gives unknow contents), >>
Could you post the content here
If you can publish this page I can help you more.
Sar1973
ASKER
Escape functions returns the HTML code replacing spaces and other characters with %20 or similar: it's not what I need. Probably the code I'm using encounters troubles durign the cycle (for ex. does not refresh the k variable in time) or misses some DIV, TD, BODY or other statement or discriminates between Ucase and Lcase.
I've read on w3schools tutorial that setTimeout variables must be named with different strings, that's why I was also asking how to combine eval syntax with setTimeout syntax.
As an alternative, I could think to insert a label or another object and give it a text value equal to the string I need to display.
leakim971
>Escape functions returns the HTML code replacing spaces and other characters with %20 or similar: it's not what I need.
Exactly (it's a literal translation): "Line 1, character 50, string constant without termination" or "Line 2, character 1, syntax error" in the 2 mentioned cases.
Open in new window
Additionaly check this : http://keith-wood.name/countdown.html