I have an html page and want to print some values from a java script function. The value needs to change often. My problem is that instead of changing "continuously" (or at least, when the code requires the value to change), it is changed only when the whole java script code has finished to execute. I am using Chrome, and haven't tested it on another browser.
Here is a minimal example reproducing the problem:
<head>
<title>Test Continuous Output</title>
</head>
<body>
<p id="data"></p>
<script>
function onClick() {
for(var i =0;i<100;i++) {
document.getElementById("data").innerHTML = i;
for(var j = 0; j < 1000; j++) {
console.log("j="+j);
}
}
}
</script>
<button onclick = "onClick()">test</button>
</body>
EDIT: posting here the code fixing this problem, using setTimeout under @Alytrem's suggestion:
<head>
<title>Test Continuous Output</title>
</head>
<body>
<p id="data"></p>
<script>
var n=0;
function log() {
document.getElementById("data").innerHTML = n;
n=n+1;
if(n<100) {
setTimeout(log,100);
}
}
function onClick() {
log();
}
</script>
<button onclick = "onClick()">test</button>
</body>