Removing a line causes the browser to freeze

I would like to further investigate this.

With this line removed from the code, the browser freezes, how come?

Is the code written wrong where that is even able to happen in the first place?

apiIsReady = true; / Line Removed:

Try clicking on a play button on the image and the browser freezes.

apiIsReady = true; / Line Added:

I was just told this:

If you remove apiIsReady = true; then it creates an infinity loop. And that’s why, the browser will freeze.

Without apiIsReady set to true, you are creating loop that adds new value to array with each iteration of that same array.

You could check if array contains that value, if not, then push it

function load_all_waitting_inits()
{
  for(var opts of waitting_inits) // new values are being added with each iteration, preventing loop to end
  {
    init(opts); // parse value of waitting_inits array
  }
}

function init(opts) {
    loadPlayer();
    if (apiIsReady) { // always false
      addVideo(opts.video, opts.playerVars || {});
    }
    else
    {
      waitting_inits.push(opts) // here you are adding values infinitely
    }
}

This resolved, fixed the issue.

Check if array includes object.

function init(opts) {
    loadPlayer();
    if (apiIsReady) {
      addVideo(opts.video, opts.playerVars || {});
    }
    else if(!waitting_inits.includes(opts)) // if array doesn't include opts then push
    {
      waitting_inits.push(opts)
    }
}

This thread is an exact copy, post by post, of OP’s SO thread:

The SO thread is solved and the post comments explains the matter and your question how to avoid it is also answered.

So what’s the purpose of this cross posting, really?

It’s not cross posting.

It was answered on there first.

And I posted the answer here.

Not everyone uses sitepoint.

There’s also Stack Overflow where you can ask questions to issues.

You’re only allowed to ask a question about an issue in 1 forum, you’re not allowed to ask it in another forum?

Is that against the rules?

Some people prefer sitepoint, others like Stack Overflow, or any number of others that are out there.

There are millions of coding forums where you can ask questions.

Are you saying that, in the millions of coding forums that are out there, I can, and am only allowed to ask a question on 1 of them?

The entire coding population isn’t signed up to sitepoint.

I also asked this question on facebook and on twitter.

I can’t do that either?

You can only ask a question in one place, and that’s it?

<off-topic>
Sorry, but it is cross posting. Though it may not be against any “rules” it is not good net etiquette. It is a discourtesy to members of both forums. Perhaps not much of a problem for short easy replies, but I imagine if a member went through the trouble of putting together a more extensive reply they would be less than pleased if they discovered that someone else had already gone through the same trouble at another forum.

Somewhat better net etiquette would be to cross reference the topics. eg. “I’ve also asked this question here” so that members could follow both topics and know where progress was at. I say “somewhat better” because it is arguable that asking people to visit two places in order to know what is going on could be a big ask.

IMHO a much better approach is to post in one or the other, and if it fails to get any traction only then post the same to another.
</off-topic>

To get back on-topic, thank you for posting the solution here.

5 Likes