我该如何延迟.then承诺?

时间:2019-01-25 22:50:33

标签: javascript asynchronous promise async-await es6-promise

我具有以下功能:

async typewrite(what, where){} -模拟有人输入文本
async typeerase(where){} -与打字相似,但模拟有人按退格键 typetimer(){} -返回承诺,随机间隔以模拟打字速度。

基本上,我想做的是在typewrite函数完成之后,我想稍等一会儿就删除文本。我尝试用setTimeout来人为地延迟它,但这似乎不起作用。


以下代码可以正常工作

typewrite('Hello world!', target).then(erase => typeerase(target));

let typetimer = (fmin, fmax) => ( 
  new Promise(res => setTimeout(res, Math.floor(Math.random() * (fmax - fmin + 1)) + fmin))
); 

const textarea = document.getElementById('textarea');

async function typewrite(what, where){
  let current_text = '';
  const typespeed_min = 50;
  const typespeed_max = 150;
  
  for (let i = 0; i < what.length; i++){
    await typetimer(typespeed_min, typespeed_max);
    current_text += what.charAt(i);
    where.innerHTML = current_text;
  }
}


async function typeerase(where){
  const erasespeed_min = 30;
  const erasespeed_max = 70;
  let current_text = where.textContent;
  
  while (current_text.length > 0){
    await typetimer(erasespeed_min, erasespeed_max)
    current_text = current_text.substring(0, current_text.length - 1);
    where.innerHTML = current_text;
  }  
}

typewrite('Hello, I am an asynchronous function!', textarea)
  .then(erase => typeerase(textarea));
html {
  font-family: 'Courier New';
  font-weight: bold;
  font-size: 15pt;
}
<p id="textarea"></p>


但是,施加人为的setTimeout()延迟似乎完全没有影响。

typewrite('Hello world!', target).then(erase => setTimeout(typeerase(target), 50000));

let typetimer = (fmin, fmax) => ( 
  new Promise(res => setTimeout(res, Math.floor(Math.random() * (fmax - fmin + 1)) + fmin))
); 

const textarea = document.getElementById('textarea');

async function typewrite(what, where){
  let current_text = '';
  const typespeed_min = 50;
  const typespeed_max = 150;
  
  for (let i = 0; i < what.length; i++){
    await typetimer(typespeed_min, typespeed_max);
    current_text += what.charAt(i);
    where.innerHTML = current_text;
  }
}


async function typeerase(where){
  const erasespeed_min = 30;
  const erasespeed_max = 150;
  let current_text = where.textContent;
  
  while (current_text.length > 0){
    await typetimer(erasespeed_min, erasespeed_max)
    current_text = current_text.substring(0, current_text.length - 1);
    where.innerHTML = current_text;
  }  
}

typewrite('Hello, I am an asynchronous function!', textarea)
  .then(erase => setTimeout(typeerase(textarea), 100000000));
html {
  font-family: 'Courier New';
  font-weight: bold;
  font-size: 15pt;
}
<p id="textarea"></p>

看来我在这里几乎错过了一些非常基本的东西,但是我似乎无法意识到我在做什么错。

2 个答案:

答案 0 :(得分:8)

简短的回答:setTimeout不能那样工作。您应该可以执行以下操作:

typewrite('Hello world!', target)
    .then(erase => setTimeout(() => typeerase(target), 50000));

长答案:您可能应该使用包装setTimeout的辅助方法以产生所需的延迟(Noitidart's answer显示了此示例)...

typewrite('Hello world!', target)
    .then(() => delay(50000))
    .then(() => typeerase(target));

...,然后使用await

await typewrite('Hello world!', target);
await delay(50000);
await typeerase(target);

答案 1 :(得分:3)

// just a class that inherits NSObject, but is not exported in the -Swift header
class Empty<T>: NSObject { }

class MyClass: Empty<String> { }