Jitsi button toggle opposite

I use jitsi meet externalApi functions to handel the meeting.

  function video() {
  console.log(document.getElementById("video1").src);
  if ((document.getElementById("video1").src).search("cam_disable.png") != -1) {
    document.getElementById("video1").src = "icons/cam_enable.png";
    document.getElementById("video2").src = "icons/cam_enable.png";
    apiObj.executeCommand('toggleVideo');
  }
  else {
    document.getElementById("video1").src = "icons/cam_disable.png";
    document.getElementById("video2").src = "icons/cam_disable.png";
    apiObj.executeCommand('toggleVideo');
  }
}

I have use this type of function i.e a button calls this function and executes but sometimes the button toggles opposite as specified. (mainly when there is a small lag.)

any solution will help me a lot.
thank you.

@damencho sir please look into this matter. I really need your help