setTimeout()不会执行

时间:2015-06-22 15:10:56

标签: javascript

我有两个使用setTimeout().的例子。这个有用:

<html>
<head>
<script type='text/javascript' src='https://www.google.com/jsapi'></script>
<script type="text/javascript">
    google.load('visualization','1',{packages:['table']});
    function start() {
            setTimeout(ShowClipboardContent, 2000);
    }

    function ShowClipboardContent() {
        var data = new google.visualization.DataTable(window.clipboardData.getData('Text'));
    var table = new google.visualization.Table(document.getElementById('div'));
    table.draw(data,{showRowNumber: true});
    }
    </script>
</head>
<body>
    <button onclick='start();'>Show text data in clipboard</button>
    <div id='div'></div>
</body>
</html>

但这不是:

<!DOCTYPE html>
<html>
<head>
    <script type='text/javascript' src='https://www.google.com/jsapi'></script>
    <script type='text/javascript'>
        google.load('visualization','1',{packages:['table']});
    </script>
    <script type='text/javascript'>
        //runs powershell and copies output onto clipboard
        function powershell(t) {
            //object that will execute powershell
            var run = new ActiveXObject("Shell.Application");
            //breaks the list of servers into array
            var servers = t.textarea.value.split('\n');
            //script that powershell will run
            var script = 'some powershell command';
            //path to powershell.exe
            var program = 'C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe';
            //putting it all together
            run.ShellExecute(program,script,'','open','1');
            setTimeout(drawTable, 10000);
        }

        //draws data table using data from clipboard
        function drawTable() {
            var data = new google.visualization.DataTable(window.clipboardData.getData('Text'));
            var table = new google.visualization.Table(document.getElementById('table'));
            table.draw(data, {showRowNumber: true});
        }
    </script>
</head>
<body>
    <div id='form'>
        <form>
            Enter name(s):<br />
            <textarea id='textarea' style='width:20%; height:500px;'></textarea><br />
            <button onclick='powershell(this.form)'>Query</button>
        </form>
    </div>
    <div id='table'></div>
</body>
</html>

重申一下:问题是setTimeout()不会对第二个代码起作用,但对第一个代码起作用,我想知道为什么,因为我认为没有区别。

1 个答案:

答案 0 :(得分:0)

尝试添加一个 console.log(drawTable); 就在你超时之前。如果它返回'function',那就好了,你的超时就可以了。如果没有返回任何内容,你的activeX脚本应该破坏代码