从同一函数内调用函数

发布于 2024-11-25 07:43:22 字数 927 浏览 0 评论 0原文

我正在将一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

嘴硬脾气大 2024-12-02 07:43:22

将字符串传递给 setTimeout 将从全局范围进行计算,其中未定义函数。相反,直接传递它,以便它将从函数的作用域进行计算,函数(显然)是在该作用域中定义的:

setTimeout( ChangeText, 2000 );

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:

setTimeout( ChangeText, 2000 );
甜中书 2024-12-02 07:43:22
setTimeout( arguments.callee, 2000 );

arguments.callee 是函数的“自指针”。最大的优点是它可以与匿名函数和命名函数一起使用。所以,对于 jQuery:

var changeAllowed = true;
var keywords      = [anything];

$(document).ready(function () {
   if(changeAllowed) {
      var keyword = keywords[ Math.floor( Math.random() * keywords.length ) ];
      $("#text").fadeOut(500).text(keyword).fadeIn(1000);
   };
   setTimeout(arguments.callee, 2000 );
});

注意:这个答案最初来自 2008 年。如今,正确的做法已经改变。正如评论中所述,您将使用命名函数表达式 (NFE),如下所示:

var changeAllowed = true;
var keywords      = [anything];

$(document).ready(function fadeKeyword() {
   if(changeAllowed) {
      var keyword = keywords[ Math.floor( Math.random() * keywords.length ) ];
      $("#text").fadeOut(500).text(keyword).fadeIn(1000);
   };
   setTimeout(fadeKeyword, 2000 );
});

arguments.callee 仍将在非严格模式下工作,但会导致错误更严格。

setTimeout( arguments.callee, 2000 );

arguments.callee is the "self pointer" for functions. Biggest advantage is that it works with anonymous functions and named functions alike. So, for jQuery:

var changeAllowed = true;
var keywords      = [anything];

$(document).ready(function () {
   if(changeAllowed) {
      var keyword = keywords[ Math.floor( Math.random() * keywords.length ) ];
      $("#text").fadeOut(500).text(keyword).fadeIn(1000);
   };
   setTimeout(arguments.callee, 2000 );
});

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:

var changeAllowed = true;
var keywords      = [anything];

$(document).ready(function fadeKeyword() {
   if(changeAllowed) {
      var keyword = keywords[ Math.floor( Math.random() * keywords.length ) ];
      $("#text").fadeOut(500).text(keyword).fadeIn(1000);
   };
   setTimeout(fadeKeyword, 2000 );
});

arguments.callee will still work in non-strict mode but cause an error in strict more.

放手` 2024-12-02 07:43:22

使用不带括号和引号的方法:setTimeout(ChangeText, 2000 );

Use it without parens and quotes: setTimeout(ChangeText, 2000 );

笙痞 2024-12-02 07:43:22

更新

当显示队列落后时,我的第一个解决方案变得不同步。这里是使用回调的更新代码,因此时机总是正确的,以及测试小提琴

var changeAllowed = true;
var keywords = ["test1", "test2", "test3", "test4", "test5"];
var hide = true;

var toggleTextFade = function() {
    if(changeAllowed) {
        var keyword =keywords[Math.floor(Math.random() * keywords.length)];

        $("#text").text(keyword).fadeIn(1000,null,function(){
            $("#text").fadeOut(500,null,toggleTextFade)});
    }               
};


$(document).ready(toggleTextFade);

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:

var changeAllowed = true;
var keywords = ["test1", "test2", "test3", "test4", "test5"];
var hide = true;

var toggleTextFade = function() {
    if(changeAllowed) {
        var keyword =keywords[Math.floor(Math.random() * keywords.length)];

        $("#text").text(keyword).fadeIn(1000,null,function(){
            $("#text").fadeOut(500,null,toggleTextFade)});
    }               
};


$(document).ready(toggleTextFade);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文