Skip to main content
Inspiring
December 31, 2023
Answered

How to avoid repeating call when the panel has not been closed in script?

  • December 31, 2023
  • 2 replies
  • 1020 views

I have written two JSX scripts,
where clicking a button in panel A calls another script from panel B.
However, the current way of calling causes the panel to be repeated every time the button is clicked on panel A.
How can I avoid repeating the call when the panel has not been closed?
For example, if panel B's script is still running and the button on panel A is clicked again without closing it,
it should not repeat the call.
Here is an example code:

var panelA = new Window("palette", "PanelA", undefined);
var button = Apanel.add("button", undefined, "Button");

button.onClick = function() {
panelB();
}
panelA.show();

function panelB () {
var panelB = new Window("palette", "PanelB", undefined);
var button2 = PanelB.add("button", undefined, "Button2");
button2.onClick = function() {
alert("this is panelB")
}
panelB.show();
}

This topic has been closed for replies.
Correct answer Airweb_AE

In this case enable the button when you close panelB

    var panelA = new Window("palette", "PanelA", undefined)
    var button = panelA.add("button", undefined, "Button")

    button.onClick = function() {
      panelB()
      this.enabled = false; // disable the button
    }
    panelA.show();

    function panelB() {
      var panelB = new Window("palette", "PanelB", undefined)
      var button2 = panelB.add("button", undefined, "Button2")
      button2.onClick = function() {
        alert("this is panelB")
      }
      panelB.onClose = function() {
        button.enabled = true
      }
      panelB.show()
    }

2 replies

Participating Frequently
December 31, 2023

To prevent repeated instantiation of Panel B, add a check within panelB function to see if the panel is already open before creating a new instance. You can use a flag variable like isPane1B0pen and set it to true when the panel is created, then check this flag before creating the panel again.

Inspiring
December 31, 2023

I have tried using the example with a flag to check if Panel B is open or not, but I want to make Panel B the focus when the flag is true. However, using

if(isPane1B0pen == false){

panelB();

} else {

panelB.show();

}

does not move the focus to Panel B after clicking on it. Have I made a mistake somewhere?

Legend
December 31, 2023

You can disable the button:

var panelA = new Window("palette", "PanelA", undefined);
var button = panelA.add("button", undefined, "Button");

button.onClick = function() {
  panelB();
  this.enabled = false; // disable the button
}
panelA.show();

function panelB() {
  var panelB = new Window("palette", "PanelB", undefined);
  var button2 = panelB.add("button", undefined, "Button2");
  button2.onClick = function() {
    alert("this is panelB")
  }
  panelB.show();
}

 

Inspiring
December 31, 2023

But if the button is disabled, when panel B closes, it won't be able to call again by push button.

Airweb_AECorrect answer
Legend
December 31, 2023

In this case enable the button when you close panelB

    var panelA = new Window("palette", "PanelA", undefined)
    var button = panelA.add("button", undefined, "Button")

    button.onClick = function() {
      panelB()
      this.enabled = false; // disable the button
    }
    panelA.show();

    function panelB() {
      var panelB = new Window("palette", "PanelB", undefined)
      var button2 = panelB.add("button", undefined, "Button2")
      button2.onClick = function() {
        alert("this is panelB")
      }
      panelB.onClose = function() {
        button.enabled = true
      }
      panelB.show()
    }