2
<script language="javascript">
// random strings
var strings = ["How is it even possible?", "Have you ever heard about this?"];
var WriteLine = function(s)
{
var end=s.length, sc=s.split("");
for(counter = 0;counter<end;counter++)
{
setTimeout(function(){document.write(sc[counter]);},20);
// ^^^^^^^^^^^
}
}
strings.forEach(WriteLine);
</script>
它返回我說:setTimeout的變量外的範圍
undefinedundefinedundefinedundefinedundefinedundefinedundefined
所以問題是變量sc
超出範圍因setTimeout
功能,我已經已嘗試使用this
它沒有超出範圍,它只是在超時發生時有錯誤的值。不,這與'this'無關。 – Bergi