Question: I have an alert that needs to be displayed to the user conditionally after they change their input. The problem I'm coming across is the alert fire rapidly multiple times in a row after they key down quickly. I want the alert to fire only once every few seconds. How can I achieve this?

```
childElem.on('change', function () {
      let currentHeightPaste2k
      let scrnwidth
      setTimeout(function () {
        let actualHeightPaste2k = parseInt($('#' + currenId2c).height()) //Height of DIV
        scrnwidth = $(window).width()
        if (scrnwidth == parseInt(1226)) {
          currentHeightPaste2k = $('#' + currenId2c).get(0).scrollHeight //Content Height of DIV
        } else {
          currentHeightPaste2k = $('#' + currenId2c).get(0).scrollHeight - 1 //Content Height of DIV
        }
        if (actualHeightPaste2k < currentHeightPaste2k) {
          _.throttle(fireError, 300)
          CKEDITOR.instances[currenId2c].execCommand('undo')
        }
      }, 100)
    })

function fireError() {
      toast.fire({
        type: 'error',
        title: 'Typing Limit Exceeded',
        text: 'You have inserted more text than the space will allow.',
        showConfirmButton: false,
        confirmButtonClass: 'cancel-btn-class',
      })
    }

```

Answer: 
You are getting an error because you are removing the button2:

```
button2.removeFromSuperview()

```

And thereafter you are trying to activate a constraints on it:

```
let topToBut1 = iconButton.topAnchor.constraint(equalTo: button1.bottomAnchor)
topToBut1.priority = UILayoutPriority(500)
topToBut1.isActive = true

```

So here you approach to remove the second button in order to the first button top constraints to be active is not really good. You can instead just check if the second button should be visible and setup the iconButton constraint accordingly, without using priority or two different constraints:

```
if secondButtonVisible {
    iconButton.topAnchor.constraint(equalTo: button2.bottomAnchor).isActive = true
} else {
    iconButton.topAnchor.constraint(equalTo: button1.bottomAnchor).isActive = true
}

```

But also you should not forget to set the second button `hidden` property to false(and not just remove it from the superview), because otherwise it will be visible under iconButton.

If you are open to an other solution, as the other answer suggests here, you can use a `UIStackView` to do all the work for you. Just add the two buttons as a subView to the `UIStackView` as follows:

```
stackView.addArrangedSubview(button1)
stackView.addArrangedSubview(button2)

```

And when you want to hide your second button you just change it's hidden property:

```
button2.isHidden = true

```

Which will hide the second button and update the layout accordingly. As per the documentation:

> 
> The stack view automatically updates its layout whenever views are
> added, removed or inserted into the arrangedSubviews array, or
> whenever one of the arranged subviews’s isHidden property changes.
> 
> 
>
Rather than adding constraints and removing button, you can use stack view and add both button in it and just show and hide them as per requirements. Stackview is enrich with so many benefits so it will handle rest of the things by it's own.