2016-02-27 47 views
-3

你可以看看这个演示,让我知道为什么我在DoSomething上得到未定义的错误?关于在jQuery中运行函数的问题setInterval

function DoSomething() { 
 
    console.log("This is Map"); 
 
} 
 

 
$(document).ready(function() { 
 
    setInterval('DoSomething()', 1000); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

我收到此错误

Uncaught ReferenceError: DoSomething is not defined

+1

发布的代码和stack-snippet工作得很好吗? – adeneo

+1

这工作正常,即使我在当前页面的控制台中运行它。 –

+0

它在我的Chrome控制台上运行正常。 –

回答

0

这工作正常,但保留必须去掉单引号语法,试试这个代码:

function DoSomething() { 
 
    document.write("This is Map"); 
 
} 
 

 
$(document).ready(function() { 
 
    setInterval(DoSomething, 1000); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>