[SOLVED] Text label have to disappear

Hello, i have a label over the enemy head that when hit show the amount of wounds, how can i make it disapear after 1 second? (text=’’)

update: function (dt) {
if (this.label.innerHTML != this.text) {
this.label.innerHTML = this.text;
}

https://playcanvas.com/editor/scene/396696

Option 1 is to use setTimeout::

var self = this;
setTimeout(function () {
    self.label.innerHTML = "";
}, 1000);

Or maybe just hide the element completely:

var self = this;
setTimeout(function () {
    self.label.style.display = 'none';
}, 1000);

setTimeout does have one major problem though, which normally causes me to recommend avoiding it. It continues to countdown and execute even when the tab is hidden/inactive. Whereas the update function stops being called when the tab becomes hidden/inactive. So you can see how that might cause issues.

A way to get around that is to do your own timing. For example:

initialize: function () {
    this.timer = 0;
},

setTextDelayed: function (text, delay) {
    this.timer = delay;
    this.text = text;
    if (delay === 0) {
        this.setText(text);
    }
},

setText: function (text) {
    this.label.innerHTML = text;
},

update: function (dt) {
    if (this.timer > 0) {
        this.timer = Math.max(this.timer - dt, 0);
        if (this.timer === 0) {
            this.setText(this.text);
        }
    }
}

There’s probably other ways to do this. Just a suggestion. :smile:

Thanks, the hide function works fine, now working on make the text disapear after a while. You helped a lot thanks