When a function or process is updating a global array,is there any chance of inconsistency while some other process is trying to access the same array in javascript in the meantime? If so,is there any prevention mechanism similar to an Operating System while trying to access a file or DBMS while a transaction is on etc? For example: function fun1 runs every 10 seconds & fun2 is triggered by a button click:
<script>
a = new Array(); //a is global
setInterval(fun1,10000);
function fun2()
{
for(var j=0;j<1000;j++)
{
a[j]=j;
}
}
function fun1()
{
for(var i=0;i<1000;i++)
{
a[i]=i+1;
}
}
</script>
<input type="button" value="Update Array" onclick="fun2()">
The above code is a sample to demonstrate this.Merely pressing the button doesn't make any sense in this context as only timing is the important matter. I only want to know the fact or whether javascript has any feature to prevent an array elements to be accessed by some other function until all the elements updated completely. Sorry, if I ask a silly question!
setTimeout, then in between something else might happen (like an ajax handler). But when your loop is running, there is no "meanwhile". Only your loop is running, any ajax handlers will be scheduled after it has finished.