Skip to main content
Participant
January 12, 2022
Answered

Dynamic text is not showing

  • January 12, 2022
  • 2 replies
  • 265 views

Hi everyone,

the issue: I have a Dynamic text that is not showing the text that I want.
The dynamic text its called "BoasTxT"

And the code that I have its the following:

var date = new Date();
hr = date.getHours();

setTimeout (function () {
if (hr >= 12)
{this.BoasTxT.text = "Bom dia";}
else
{this.BoasTxT.text = "Boa tarde";}
}, 1);


What Im doing wrong?
Thanks in advanced.

    This topic has been closed for replies.
    Correct answer JoãoCésar17023019

    Hi.

     

    It's a context problem. If you log the keyword this to the console you'll see it refers to the window object and not to the main timeline.

     

    There are at least 3 ways of solving this.

     

    1 - Creating a global reference.

     

    var root = this;
    var date = new Date();
    
    hr = date.getHours();
    
    setTimeout(function()
    {
    	if (hr >= 12)
    		root.BoasTxT.text = "Bom dia";
    	else
    		root.BoasTxT.text = "Boa tarde";
    }, 1);

     

     

    2 - Binding.

     

    var date = new Date();
    
    hr = date.getHours();
    
    setTimeout(function()
    {
    	if (hr >= 12)
    		this.BoasTxT.text = "Bom dia";
    	else
    		this.BoasTxT.text = "Boa tarde";
    }.bind(this), 1);

     

     

    3 - Passing the current timeline as an argument to the setTimeout method.

     

    var date = new Date();
    
    hr = date.getHours();
    
    setTimeout(function(root)
    {
    	if (hr >= 12)
    		root.BoasTxT.text = "Bom dia";
    	else
    		root.BoasTxT.text = "Boa tarde";
    }, 1, this);

     

     

    Anyway, I don't think you need the setTimeout method at all. You can just change the text property of regular dynamic text fields right away. This setTimeout approach is generally used as workaround for component text fields (Window > Components > User Interface).

     

    I hope it helps.

     

    Regards,

    JC

    2 replies

    JoãoCésar17023019
    Community Expert
    JoãoCésar17023019Community ExpertCorrect answer
    Community Expert
    January 12, 2022

    Hi.

     

    It's a context problem. If you log the keyword this to the console you'll see it refers to the window object and not to the main timeline.

     

    There are at least 3 ways of solving this.

     

    1 - Creating a global reference.

     

    var root = this;
    var date = new Date();
    
    hr = date.getHours();
    
    setTimeout(function()
    {
    	if (hr >= 12)
    		root.BoasTxT.text = "Bom dia";
    	else
    		root.BoasTxT.text = "Boa tarde";
    }, 1);

     

     

    2 - Binding.

     

    var date = new Date();
    
    hr = date.getHours();
    
    setTimeout(function()
    {
    	if (hr >= 12)
    		this.BoasTxT.text = "Bom dia";
    	else
    		this.BoasTxT.text = "Boa tarde";
    }.bind(this), 1);

     

     

    3 - Passing the current timeline as an argument to the setTimeout method.

     

    var date = new Date();
    
    hr = date.getHours();
    
    setTimeout(function(root)
    {
    	if (hr >= 12)
    		root.BoasTxT.text = "Bom dia";
    	else
    		root.BoasTxT.text = "Boa tarde";
    }, 1, this);

     

     

    Anyway, I don't think you need the setTimeout method at all. You can just change the text property of regular dynamic text fields right away. This setTimeout approach is generally used as workaround for component text fields (Window > Components > User Interface).

     

    I hope it helps.

     

    Regards,

    JC

    Participant
    January 12, 2022

    Thank you for the clarifications! (issue solved)

    Best regards,
    MC

    JoãoCésar17023019
    Community Expert
    Community Expert
    January 12, 2022

    You're welcome!

    Participant
    January 12, 2022

    Oh forgot to Say:

    Software = Animate (HTML canvas)