简体   繁体   English

我正在尝试每 2 秒将一个字符添加到一个字符串中,但它不起作用,控制台中没有错误。 视图

[英]I'm trying adding a to a string a char every 2 seconds, but it diesn't work, no errors in a console. Vue

I know task is super simple.我知道任务非常简单。 But I do not why it's not working.但我不知道为什么它不起作用。 Here is a code below how I implemented, as far as I checked this.showStr += this.mainStr.charAt(i) there is no mistake in this part is just something wrong with connection loop and setTimer.这是我如何实现的代码,据我检查this.showStr += this.mainStr.charAt(i)这部分没有错误,只是连接循环和 setTimer 有问题。 Does anybody see an error?有人看到错误吗?

  data(){
      return {
        mainStr: "Hello, my name is Eldar and I'm web-developer",
        showStr: ''
      }
  },
  methods:{
      showString() {
        for (let i = 0; i < this.mainStr.length; ++i) {
          this.delay(i);
        }
      },
    delay(i){
      function delay() {
        setTimeout(() => {
          this.showStr += this.mainStr.charAt(i)
        }, 2000)
      }
    }
  },
  mounted(){
      this.showString();
  }

Ohh I'm sorry I published the wrong code.哦,对不起,我发布了错误的代码。 Here is a code which doesnt work这是一个不起作用的代码

<template>
    <p>{{ showStr }}</p>
</template>

<script>
    export default {
        name: "GreetingTitle.vue",
      data(){
          return {
            mainStr: "Hello, my name is Eldar and I'm web-developer",
            showStr: ''
          }
      },
      methods:{
          showString() {
            for (let i = 0; i < this.mainStr.length; ++i) {
              this.delay(i);
            }
          },
        delay(i){
            setTimeout(() => {
              this.showStr += this.mainStr.charAt(i)
            }, 2000)
        }
      },
      mounted(){
          this.showString();
      }

    }
</script>

Your mistake is that you set all timers in one time.您的错误是您一次性设置了所有计时器。 You need to set different timers for calling your function time by time with interval, like that:您需要设置不同的计时器以按时间间隔调用 function,如下所示:

 const mainStr = "Hello, my name is Eldar and I'm web-developer"; let showStr = ''; for (let i = 0; i < mainStr.length; ++i) { delay(i); } function delay(i){ setTimeout(() => { showStr += mainStr.charAt(i) console.log(showStr); }, 300 * i) }

UP: Vue version (for dummies): UP:Vue 版本(用于傻瓜):

 new Vue({ name: "GreetingTitle.vue", data(){ return { mainStr: "Hello, my name is Eldar and I'm web-developer", showStr: '' } }, methods:{ showString() { for (let i = 0; i < this.mainStr.length; ++i) { this.delay(i); } }, delay(i){ setTimeout(() => { this.showStr += this.mainStr.charAt(i) }, 300 * i) } }, mounted(){ this.showString(); } }).$mount('#container');
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id='container'> <p>{{ showStr }}</p> </div>

Ohh I'm sorry I published the wrong code.哦,对不起,我发布了错误的代码。 Here is a code which doesnt work这是一个不起作用的代码

<template>
    <p>{{ showStr }}</p>
</template>

<script>
    export default {
        name: "GreetingTitle.vue",
      data(){
          return {
            mainStr: "Hello, my name is Eldar and I'm web-developer",
            showStr: ''
          }
      },
      methods:{
          showString() {
            for (let i = 0; i < this.mainStr.length; ++i) {
              this.delay(i);
            }
          },
        delay(i){
            setTimeout(() => {
              this.showStr += this.mainStr.charAt(i)
            }, 2000)
        }
      },
      mounted(){
          this.showString();
      }

    }
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我试图在构造器中每秒调用一次方法15秒,但setInterval似乎不起作用 - I'm trying to call a method in a constructer every second for 15 seconds but setInterval doesn't seem to work 我想从 api 获取数据(姓名和电子邮件)。 我的程序已成功编译,但我在控制台中遇到错误。 https://randomuser.me/api - I am suppose to fetch data (name & email) from api. My program is compiled sucessfully but I'm getting errors in console. https://randomuser.me/api 控制台每40秒输出一次,但间隔不起作用 - Console output one time every 40 seconds with interval doesn't work 尝试在 Vue 3 中导入节点模块时遇到错误(我没有使用打字稿) - Encountering errors while trying to import node modules in Vue 3 (I'm not using typescript) 当我尝试移动跨度时,appendChild不起作用 - appendChild doesn't work when I'm trying to move span 反应本地 我正在添加admob代码,但是它不起作用 - react native; I'm adding admob code but it doesn't work 我正在尝试使用 removeEventDelegate 删除事件处理程序,但它不起作用 - I'm trying to remove event handler with removeEventDelegate but it doesn't work React Search - 我正在尝试创建一个过滤器,但它不起作用 - React Search - I'm trying to create a filter and it doesn't work 我正在尝试使用jquery从下拉列表中获取选定的值。 由于某些原因,所选选项未打印到控制台。 Helppss - I am trying to get the selected value from a dropdown list using jquery. For some reason, the option selected is not printing to the console. Helppss 我试图在控制台上显示“嗨,我是大卫,数字是偶数”,但每次我运行代码时,“偶数”都会显示为数字 - I'm trying to get "Hi I am David , and the number is even" shown on the console, but every time I ran the code "even" is shown as a number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM