Javascript, setTimeout and setInterval

Repeated Events: Timeout or Interval

Timeout

<script>
function showHints() {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
setTimeout(showHints, 1000);
   }
        };
        xmlhttp.open("GET", "asdhint.txt", true);
        xmlhttp.send();
}
setTimeout(showHints, 1000); // 1000 = 1sec
</script>

Interval

<script>
function showHints() {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
        };
        xmlhttp.open("GET", "asdhints.txt", true);
        xmlhttp.send();
}
setInterval(showHints,1000);
</script>

Comments

Popular Posts