最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to emulate setTimeout() in Blazor? - Stack Overflow

programmeradmin2浏览0评论

I'm trying to replicate a simple JS method but in Blazor. The idea is to type out every char in a given word/sentence/etc. W3Schools has a great example of a quick way to do this. So given their example how does one do the same thing in C# as in JS?

var i = 0;
var txt = 'Lorem ipsum dummy text blabla.';
var speed = 50;

function typeWriter() {
  if (i < txt.length) {
    document.getElementById("demo").innerHTML += txt.charAt(i);
    i++;
    setTimeout(typeWriter, speed);
  }
}

I'm sure this isn't that good a way to do it, hence being here, but I think that there has to be a way via Threading to do the same thing as setTimeout just not sure how to go about doping that. Also trying to update the HTML via a variable in the code-behind.

Below is what I have tried so far:

HTML:

<div>
  @message
</div>

Blazor Code Behind:

using System.Threading;

namespace MyApp.Pages
{
    public partial class Index
    {
        private static string message = "";
        private static string toType = "Lorem ipsum dummy text blabla.";
        private static int charIndex = 0;

        protected override void OnInitialized()
        {
            int seconds = 5000;
            var timer = new Timer(TypeMessage, null, 0, seconds);
        }

        protected static void TypeMessage(object o)
        {
            if (charIndex < toType.Length)
            {
                message = message + toType[charIndex];
                charIndex++;
            }
        }
    }
}

Any advice on what's the best way to do this is appreciated!

I'm trying to replicate a simple JS method but in Blazor. The idea is to type out every char in a given word/sentence/etc. W3Schools has a great example of a quick way to do this. So given their example how does one do the same thing in C# as in JS?

var i = 0;
var txt = 'Lorem ipsum dummy text blabla.';
var speed = 50;

function typeWriter() {
  if (i < txt.length) {
    document.getElementById("demo").innerHTML += txt.charAt(i);
    i++;
    setTimeout(typeWriter, speed);
  }
}

I'm sure this isn't that good a way to do it, hence being here, but I think that there has to be a way via Threading to do the same thing as setTimeout just not sure how to go about doping that. Also trying to update the HTML via a variable in the code-behind.

Below is what I have tried so far:

HTML:

<div>
  @message
</div>

Blazor Code Behind:

using System.Threading;

namespace MyApp.Pages
{
    public partial class Index
    {
        private static string message = "";
        private static string toType = "Lorem ipsum dummy text blabla.";
        private static int charIndex = 0;

        protected override void OnInitialized()
        {
            int seconds = 5000;
            var timer = new Timer(TypeMessage, null, 0, seconds);
        }

        protected static void TypeMessage(object o)
        {
            if (charIndex < toType.Length)
            {
                message = message + toType[charIndex];
                charIndex++;
            }
        }
    }
}

Any advice on what's the best way to do this is appreciated!

Share Improve this question asked Aug 22, 2020 at 4:11 CoderLeeCoderLee 3,5196 gold badges32 silver badges65 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Try something like

Task.Run(async() =>
{
  for (int i=1; i <= Message.Length; i++) 
  {
      TextToShow = Message.Substring(0, i);
      InvokeAsync(StateHasChanged);
      await Task.Delay(500);
  }
}) ;

Calling InvokeAsync(StateHasChanged) should work :

...
        protected static void TypeMessage(object o)
        {
            if (charIndex < toType.Length)
            {
                message = message + toType[charIndex];
                charIndex++;
                InvokeAsync(StateHasChanged);
            }
        }
发布评论

评论列表(0)

  1. 暂无评论