Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在chrome中未正确显示脚本_Javascript_Google Chrome - Fatal编程技术网

Javascript 在chrome中未正确显示脚本

Javascript 在chrome中未正确显示脚本,javascript,google-chrome,Javascript,Google Chrome,我使用的是一个短JavaScript函数,在firefox和IE中正常工作,但在chrome中显示不正确。 该函数只是在页面上键入一个文本块。在FF和IE中,它的输入很好。在Chrome中,它中途停止,不会结束 剧本是: var index = 0; var text ='text to get typed goes here'; function type() { document.getElementById('screen').innerHTML += text.charAt(i

我使用的是一个短JavaScript函数,在firefox和IE中正常工作,但在chrome中显示不正确。 该函数只是在页面上键入一个文本块。在FF和IE中,它的输入很好。在Chrome中,它中途停止,不会结束

剧本是:

var index = 0;
var text ='text to get typed goes here';

function type() {
    document.getElementById('screen').innerHTML += text.charAt(index);
    index += 1;
    var t = setTimeout('type()',50);
}
它在页面上被

div align="left" class="normtext" id='screen'

有人知道为什么这在chrome中不起作用吗?谢谢。

您的setTimeout需要一个闭包,因为它在全局/窗口范围内调用其函数。这将阻止函数在某些情况下被识别

以下是工作代码:

var index = 0;
var text ='text to get typed goes here';

function type() {
    if (index < text.length) { // Don't go on typing after finishing.
        document.getElementById('screen').innerHTML += text.charAt(index);
        index += 1;
        var t = setTimeout(function () {
            type();
        },50);
    }
}
type();
var指数=0;
var text='text要键入的文本在此处';
函数类型(){
如果(index

您还可以在这里看到一个示例:

您只需在Chrome中打开控制台,看看会出现什么错误。使用setTimeout(type,50);,当字符串参数调用时,使用伪装的evalmake a在这里可以正常工作:使用@Misiur suggestion)