0

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!

4
  • 1
    JavaScript is single-threaded, there is no other script running at the same time Commented Dec 21, 2013 at 13:37
  • say, e = document.getElementsByTagName('span'); This global e stores reference to all the span elements in the document.If I add dynamically (AJAX) one more span,then e.length is incremented by 1.@Bergi Commented Dec 21, 2013 at 14:53
  • continued form above:Also I have a function that collects the values of span every 60 secs by using a loop (i=0;i<e.length;i++){}, i.e.,from the 1st to the last span.My question is:at the start of the function value of e.length is say 10,in the meanwhile I added another span,i.e,e.length=11,then e[0] will be the newly added span or the span before adding the new one for the function? Isn't there any ambiguity? Confused! Commented Dec 21, 2013 at 14:54
  • 1
    Well, if you wait 60s by using 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. Commented Dec 21, 2013 at 19:01

1 Answer 1

1

Javascript is not a multi-threaded language. So all you do is happening in sequence. Whether it's a feature or not is arguable, but it does prevent writing from two different processes :)

Sign up to request clarification or add additional context in comments.

2 Comments

setTimeout and setInterval not always execute their code in exactly X milliseconds because, from my understanding, the calls are queued and executed after code that has been executing X seconds after is done.
is this same as with a variable in javascript? @Lex Podgorny

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.