超时和滚动问题

Issues with settimeout and scrolling

本文关键字:问题 滚动 超时      更新时间:2023-09-26

我对编码世界有些陌生,我(可能很愚蠢)决定给我的朋友做一款"选择你自己的冒险游戏"作为生日礼物。简而言之,我遇到了一个问题,需要一个时间延迟来给用户阅读文本和向下滚动页面的时间,因为否则窗口不会滚动。我最终尝试使用.settimeout()来创建设置延迟,但问题是,在发生这种情况时,settimeout之后的代码本身会运行,这会给用户带来某种死胡同。它看起来像这样:

  //A bunch of text and story up here 
  window.setTimeout( function () { 'prompt to get variable for if statement' }, delay );
  if(true)
    { //code to run}
  else
   {//code to run}

接下来是其他几个提示及其相应的if/else语句。

我假设代码在延迟发生时正在运行,我想知道是否存在更好的方法。我知道Javascript中没有暂停功能,但我认为必须有某种方法允许用户在弹出另一个提示之前向下滚动。非常感谢。

if-else语句将立即运行。你应该把它放在你的setTimeout函数中,就像这样:

//A bunch of text and story up here 
window.setTimeout( function () {
  //do some magic

  if(true)
    { //code to run}
  else
   {//code to run}
}, delay );

试试这个:

var donext = false;
window.setTimeout( function() {
  donext = true;
}, delay);
if(donext)
{ //code to run}
else
{ //code to run}