暂停加载 HTML 元素

huangapple go评论51阅读模式
英文:

Pause loading html elements

问题

我有一个Dreamweaver JS代码(不是我的),来自2004年。它应该以一些延迟输入符号。但它没有。

<script type="text/javascript">
<!--
  // alert() // if I put an alert, everyting works correct
  theDate = new Date();
  thetime = Date().toString();
  curtime = new Date();
  lasttime = new Date();
  var val = 1000;
  var newval = 0;
  function delay(val) {
    var t3 = 0; 
    lasttime = new Date()
    t1 = lasttime.getTime();
    t3 = t1 + val;
    curtime = new Date();
    t2 = curtime.getTime();
    while (t2 < t3) {
      curtime = new Date();
      t2 = curtime.getTime();
    }
  }
  document.wait = delay;
//-->
</script><script>
val = parseInt(top.frames[0].myform.mytext.value);
document.wait(val);
</script><script>
document.wait(val);
</script><script>
document.wait(val);
</script><script>
//val=100;
document.wait(val);
</script>
<B><BASEFONT SIZE="6" FACE="Arial Cyr" COLOR="#8000FF">&nbsp;
 # &nbsp;&nbsp;&nbsp;<script> document.wait(val);</script>&nbsp;
, &nbsp;&nbsp;&nbsp;<script> document.wait(val);</script> &quot;<script> document.wait(val);</script>
 &amp; &nbsp;&nbsp;&nbsp;<script> document.wait(val);</script>
  &#167; &nbsp;&nbsp;&nbsp;<script> document.wait(val);</script>

但如果在此代码之前放置了警告框(在示例中已注释),它将正常工作。不是在所有浏览器中都有效(例如,在IE中不起作用),但它会起作用。如何在没有警告的情况下修复它,以及为什么警告使代码正常工作?

英文:

I have a Dream weaver js code (not mine) from 2004 year. It should type symbols with some delay. But it doesn't.

&lt;script type=&quot;text/javascript&quot;&gt;
&lt;!--
// alert() // if I put an alert, everyting works correct
theDate=new Date();
thetime=Date().toString();
curtime=new Date();
lasttime=new Date();
var val=1000;
var newval=0;
function delay(val) {
var t3=0; 
lasttime=new Date()
t1=lasttime.getTime();
t3=t1+val;
curtime=new Date();
t2=curtime.getTime();
while (t2 &lt; t3) {
curtime=new Date();
t2=curtime.getTime();
}
}
document.wait=delay;
&lt;!---&gt;
&lt;/script&gt;&lt;script&gt;
val=parseInt(top.frames[0].myform.mytext.value);
document.wait(val);
&lt;/script&gt;&lt;script&gt;
document.wait(val);
&lt;/script&gt;&lt;script&gt;
document.wait(val);
&lt;/script&gt;&lt;script&gt;
//val=100;
document.wait(val);
&lt;/script&gt;
&lt;B&gt;&lt;BASEFONT SIZE=&quot;6&quot; FACE=&quot;Arial Cyr&quot; COLOR=&quot;#8000FF&quot;&gt;&amp;nbsp;
# &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;script&gt; document.wait(val);&lt;/script&gt;&amp;nbsp;
, &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;script&gt; document.wait(val);&lt;/script&gt; &amp;quot;&lt;script&gt; document.wait(val);&lt;/script&gt;
&amp;amp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;script&gt; document.wait(val);&lt;/script&gt;
&#167; &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;script&gt; document.wait(val);&lt;/script&gt;

But if I put alert before this code (it is commented in the sample) it will work. Not in all browsers (it will not work in IE, for example), but it will. How can I fix it without alert and why alert makes code work right?

答案1

得分: 1

根据Yogi的建议,您应该使用超时或async函数来将字符呈现到元素中。

以下是一个使用超时的版本:

const typeWriter = (elementOrSelector, text, speed = 10) => {
  const el = typeof elementOrSelector === 'string'
    ? document.querySelector(elementOrSelector)
    : elementOrSelector;
  let i = 0;
  const fn = () => {
    if (i < text.length) {
      el.textContent += text[i++];
      setTimeout(fn, speed);
    }
  }
  fn();
};

typeWriter('#demo-slow', 'Lorem ipsum dummy text blabla.', 100);
typeWriter('#demo-fast', 'Lorem ipsum dummy text blabla.', 50);
typeWriter('#demo-faster', 'Lorem ipsum dummy text blabla.', 25);

以下是一个使用asyncsleep的版本:

const sleep = async (ms) => new Promise((res) => setTimeout(res, ms));

const typeWriterAsync = async (elementOrSelector, text, speed = 10) => {
  const el = typeof elementOrSelector === 'string'
    ? document.querySelector(elementOrSelector)
    : elementOrSelector;
  for (let i = 0; i < text.length; i++) {
    await sleep(speed);
    el.textContent += text[i];
  }
};

// 一次性全部输出,与上面的方式相同
typeWriterAsync('#demo-slow', 'Lorem ipsum dummy text blabla.', 100);
typeWriterAsync('#demo-fast', 'Lorem ipsum dummy text blabla.', 50);
typeWriterAsync('#demo-faster', 'Lorem ipsum dummy text blabla.', 25);

// 您还可以链式调用
(async() => {
  await typeWriterAsync('#demo-slow-chain', 'Lorem ipsum dummy text blabla.', 100);
  await typeWriterAsync('#demo-fast-chain', 'Lorem ipsum dummy text blabla.', 50);
  await typeWriterAsync('#demo-faster-chain', 'Lorem ipsum dummy text blabla.', 25);
  console.log('完成!');
})();

希望这对您有所帮助!

英文:

As Yogi suggested, you should use a timeout or async functions to render characters to an element.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const typeWriter = (elementOrSelector, text, speed = 10) =&gt; {
const el = typeof elementOrSelector === &#39;string&#39;
? document.querySelector(elementOrSelector)
: elementOrSelector;
let i = 0;
const fn = () =&gt; {
if (i &lt; text.length) {
el.textContent += text[i++];
setTimeout(fn, speed);
}
}
fn();
};
typeWriter(&#39;#demo-slow&#39;, &#39;Lorem ipsum dummy text blabla.&#39;, 100);
typeWriter(&#39;#demo-fast&#39;, &#39;Lorem ipsum dummy text blabla.&#39;, 50);
typeWriter(&#39;#demo-faster&#39;, &#39;Lorem ipsum dummy text blabla.&#39;, 25);

<!-- language: lang-css -->

p { margin: 0; }

<!-- language: lang-html -->

&lt;p id=&quot;demo-slow&quot;&gt;&lt;/p&gt;
&lt;p id=&quot;demo-fast&quot;&gt;&lt;/p&gt;
&lt;p id=&quot;demo-faster&quot;&gt;&lt;/p&gt;

<!-- end snippet -->

Here is an async version with a sleep:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const sleep = async (ms) =&gt; new Promise((res) =&gt; setTimeout(res, ms));
const typeWriterAsync = async (elementOrSelector, text, speed = 10) =&gt; {
const el = typeof elementOrSelector === &#39;string&#39;
? document.querySelector(elementOrSelector)
: elementOrSelector;
for (let i = 0; i &lt; text.length; i++) {
await sleep(speed);
el.textContent += text[i];
}
};
// All at once, like above
typeWriterAsync(&#39;#demo-slow&#39;, &#39;Lorem ipsum dummy text blabla.&#39;, 100);
typeWriterAsync(&#39;#demo-fast&#39;, &#39;Lorem ipsum dummy text blabla.&#39;, 50);
typeWriterAsync(&#39;#demo-faster&#39;, &#39;Lorem ipsum dummy text blabla.&#39;, 25);
// You can also chain
(async() =&gt; {
await typeWriterAsync(&#39;#demo-slow-chain&#39;, &#39;Lorem ipsum dummy text blabla.&#39;, 100);
await typeWriterAsync(&#39;#demo-fast-chain&#39;, &#39;Lorem ipsum dummy text blabla.&#39;, 50);
await typeWriterAsync(&#39;#demo-faster-chain&#39;, &#39;Lorem ipsum dummy text blabla.&#39;, 25);
console.log(&#39;Done!&#39;);
})();

<!-- language: lang-css -->

p { margin: 0; }

<!-- language: lang-html -->

&lt;p id=&quot;demo-slow&quot;&gt;&lt;/p&gt;
&lt;p id=&quot;demo-fast&quot;&gt;&lt;/p&gt;
&lt;p id=&quot;demo-faster&quot;&gt;&lt;/p&gt;
&lt;hr /&gt;
&lt;p id=&quot;demo-slow-chain&quot;&gt;&lt;/p&gt;
&lt;p id=&quot;demo-fast-chain&quot;&gt;&lt;/p&gt;
&lt;p id=&quot;demo-faster-chain&quot;&gt;&lt;/p&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月14日 03:12:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76682557.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定