从同一函数内调用函数
我正在将一些 JavaScript 代码转换为 jQuery,但已经停止了, 这是代码...
HTML:
<div id="text"></div>
JavaScript:
keywords = [ "one", "two", "three"]
var allowed = true;
function ChangeText ( ) {
if(allowed)
{
var keyword = keywords[ Math.floor( Math.random() * keywords.length ) ]
document.getElementById( "text" ).innerHTML = keyword;
}
setTimeout( "ChangeText()", switchTime );
}
ChangeText();
jQuery:
var changeAllowed = true;
$(document).ready(function ChangeText() {
if(changeAllowed)
{
$("#text").fadeOut(500);
var keyword = keywords[ Math.floor( Math.random() * keywords.length ) ];
$("#text").text(keyword).fadeIn(1000);
};
setTimeout( "ChangeText()", 2000 );
});
ChangeText();
它应该做的是每隔 2 秒左右淡出文本,然后用新字符串返回(刷新时执行此操作),jQuery 效果正在工作,它似乎只是 setTimeout()或者我没有正确命名该函数。
I'm converting some JavaScript code to jQuery and have come to a halt,
Heres the code...
HTML:
<div id="text"></div>
JavaScript:
keywords = [ "one", "two", "three"]
var allowed = true;
function ChangeText ( ) {
if(allowed)
{
var keyword = keywords[ Math.floor( Math.random() * keywords.length ) ]
document.getElementById( "text" ).innerHTML = keyword;
}
setTimeout( "ChangeText()", switchTime );
}
ChangeText();
jQuery:
var changeAllowed = true;
$(document).ready(function ChangeText() {
if(changeAllowed)
{
$("#text").fadeOut(500);
var keyword = keywords[ Math.floor( Math.random() * keywords.length ) ];
$("#text").text(keyword).fadeIn(1000);
};
setTimeout( "ChangeText()", 2000 );
});
ChangeText();
What it should do is fade the text out then back in with a new string (which it does on refresh) every 2 seconds or so, The jQuery effects are working, it just seems to be the setTimeout() or I've not named the function properly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将字符串传递给
setTimeout
将从全局范围进行计算,其中未定义函数。相反,直接传递它,以便它将从函数的作用域进行计算,函数(显然)是在该作用域中定义的:Passing a string to
setTimeout
will evaluate from the global scope, in which the funcion is not defined. Pass it directly instead, so that it will evaluate from the function's scope, in which the function (obviously) is defined:arguments.callee
是函数的“自指针”。最大的优点是它可以与匿名函数和命名函数一起使用。所以,对于 jQuery:注意:这个答案最初来自 2008 年。如今,正确的做法已经改变。正如评论中所述,您将使用命名函数表达式 (NFE),如下所示:
arguments.callee
仍将在非严格模式下工作,但会导致错误更严格。arguments.callee
is the "self pointer" for functions. Biggest advantage is that it works with anonymous functions and named functions alike. So, for jQuery:Note: This answer is from 2008 originally. Nowadays the proper way to do this has changed. As noted in the comments you'd use a named function expression (NFE), like this:
arguments.callee
will still work in non-strict mode but cause an error in strict more.使用不带括号和引号的方法:
setTimeout(ChangeText, 2000 );
Use it without parens and quotes:
setTimeout(ChangeText, 2000 );
更新
当显示队列落后时,我的第一个解决方案变得不同步。这里是使用回调的更新代码,因此时机总是正确的,以及测试小提琴:
UPDATE
My first solution got out of synch when the display queue got behind. Here is updated code that uses callbacks so the timing is always right, along with the test fiddle: