这里有些失落,对JavaScript来说还很新。
仅当函数includeHTML()
的值等于或大于100时,我才尝试播放声音。
includeHTML()
的值在一个正在读取的文本文件中,可以正常工作,但是如果我写入includeHTML()
的值,则返回的值不确定,因此会播放声音。
<!DOCTYPE html>
<html>
<script>
function includeHTML() {
var z, i, elmnt, file, xhttp;
z = document.getElementsByTagName("*");
for (i = 0; i < z.length; i++) {
elmnt = z[i];
file = elmnt.getAttribute("mqm-include-html");
if (file) {
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 200) {elmnt.innerHTML = this.responseText;}
if (this.status == 404) {elmnt.innerHTML = "Count not found.";}
elmnt.removeAttribute("mqm-include-html");
includeHTML();
}
}
xhttp.open("GET", file, true);
xhttp.send();
return;
}
}
};
</script>
<body>
<div id ="mqmcount" mqm-include-html="/MQM/mqmcount.txt"></div>
<script>
includeHTML();
document.getElementById('mqmcount').innerHTML = mqmcount;
var warning = new Audio('warning.mp3');
if (mqmcount >= "100"){
warning.play();
}
</script>
</body>
</html>