简体   繁体   English

如何在 JavaScript 的条件下更改间隔时间?

[英]How to change interval time under a condition in JavaScript?

I need a function that will print letters one by one but make pauses after ",".我需要一个 function 一个一个打印字母,但在“,”之后暂停。 I tried to do it like this but it didn't work:我试图这样做,但没有奏效:

var span = document.getElementById('text')
function print(string){
  var i = 0
  var time = 50
  var timer = setInterval(function(){
    span.innerHTML += string[i++]
    if(string[i] == ","){
      time = 150
    }else{
      time = 50
    }  
  },time)
}

I am not sure you can change the time of setTimeinterval once defined.我不确定一旦定义就可以更改setTimeinterval的时间。 It is better to use a setTimeout .最好使用setTimeout I'm using recursive function to achieve the desired -我正在使用递归 function 来实现所需的 -

function print(string, i){
  var time = 50
  if(string[i]==",") time = 1000
  setTimeout(function(){
    span.innerHTML += string[i]
    if(i<string.length-1) print(string,++i)
  },time)

}

complete example can be found here完整的例子可以在这里找到

First, I don't think setInterval is what you're looking for.首先,我不认为setInterval是你要找的。 Since it seems you just want to print each character with a delay, setTimeout would work best in this situation.由于您似乎只想延迟打印每个字符,因此setTimeout在这种情况下效果最好。

I would break this up so that you can focus on printing a single character at a time, and then you can call that in your main print function for each character in the string.我会将其分解,以便您可以专注于一次打印一个字符,然后您可以在主print function 中为字符串中的每个字符调用它。

This is how I would do something like this, using Promises:这就是我使用 Promises 做这样的事情的方式:

const printCharacter = (ch, idx) => new Promise(resolve => {
    let time = 50 * idx;

    if(ch === ',') {
        time = 150 + time * idx;
    }

    setTimeout(() => {
        const span = document.getElementById('text');
        span.innerHTML += ch;
        resolve();
    }, time)
});

const print = async string => {
    for(let i = 0; i < string.length; i++) {
        await printCharacter(string[i], i);
    }
};

There are some bugs that could present itself here, such as the timing for multiple commas present in your base string.这里可能会出现一些错误,例如基本字符串中出现多个逗号的时间。 This is just my first iteration.这只是我的第一次迭代。

Instead of using an interval, I recommend using a for loop to iterate the string and a Promise to pause as long as desired.我建议不要使用间隔,而是使用 for 循环来迭代字符串,并使用 Promise 来根据需要暂停。

Side note, use textContent instead of innerHTML .旁注,使用textContent而不是innerHTML The former is like myCar.cangeTires(); myCar.closeDoors()前者就像myCar.cangeTires(); myCar.closeDoors() myCar.cangeTires(); myCar.closeDoors() , the latter is like myCar.do('change tires'); myCar.do('close doors'); myCar.cangeTires(); myCar.closeDoors() ,后者类似于myCar.do('change tires'); myCar.do('close doors'); myCar.do('change tires'); myCar.do('close doors'); . .

 let sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); var span = document.getElementById('text'); let print = async string => { span.textContent = ''; for (let c of string) { span.textContent += c; await sleep(c === ','? 500: 50); } }; print('my text, and a comma');
 <span id="text"></span>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM