Ron
Ron

Reputation: 1894

Listen to Event When Array Is Populate (Array.push(object))

I need to listen to a Javascript array and invoke an event once the array has been populate ( have at lest one object in that array)

(Javascript, Jquery it doesn't matter to me)

How can I do it ?

Upvotes: 0

Views: 697

Answers (2)

Pushker Yadav
Pushker Yadav

Reputation: 856

 Array.prototype.myPush=function(value){
       this.push(value);
       alert('value added');
    
    }
    var arr=[1,2];
    arr.myPush(3)

Upvotes: 2

void
void

Reputation: 36703

METHOD 1 You can modify the code where you are pushing the array

In your code where you are pushing the value inside the array add the piece of code to trigger the event.

var arr = [];
arr.push(1);
// Trigger Event Here

METHOD 2 You can not modify the code where you are pushing the array

// arr is the global array you want to check
var intArr = setInterval(function(){
  if(arr.length>0)
  {
    // Trigger Event.
    clearInterval(intArr); // To stop setInterval() when event is triggered
  }
},100);

Upvotes: 0

Related Questions