setTimeout 只运行一次?

发布于 2024-12-11 21:02:31 字数 564 浏览 0 评论 0原文

function slide()
{
    if($('.current').is(':last-child')){
        $('.current').removeClass('.current');
        $('#imgholder').first().addClass('.current');
        $('#imgholder').animate({left: '3920px'});
    }
    else{
        $nxt=$(".current");
        $(".current").removeClass("current");
        $nxt.next().addClass("current");
        $('#imgholder').animate({left: '-=980'},{duration: 'slow', easing: 'easeOutBounce' });
        }
}
var loop_handle= setTimeout("slide()",'3000');

我已将此代码放在标头部分中,并且 setTimeout 仅运行一次。

function slide()
{
    if($('.current').is(':last-child')){
        $('.current').removeClass('.current');
        $('#imgholder').first().addClass('.current');
        $('#imgholder').animate({left: '3920px'});
    }
    else{
        $nxt=$(".current");
        $(".current").removeClass("current");
        $nxt.next().addClass("current");
        $('#imgholder').animate({left: '-=980'},{duration: 'slow', easing: 'easeOutBounce' });
        }
}
var loop_handle= setTimeout("slide()",'3000');

I have put this code in header section and the setTimeout runs only once.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

我乃一代侩神 2024-12-18 21:02:31

setTimeout 应该只运行一次。您正在寻找setInterval

var loop_handle = setInterval(slide, 3000);

另外,第二个参数应该是数字,而不是字符串。当函数调用不需要任何参数时,最好引用函数而不是使用字符串。字符串将被转换为函数。该函数将在窗口范围内执行。

  setInterval("slide()", 3000);
//becomes
  setInterval(Function("slide();"), 3000);

setTimeout should only run once. You're looking for setInterval.

var loop_handle = setInterval(slide, 3000);

Also, the second argument should be a number, not a string. When the function call doesn't require any arguments, it's better to reference to the function instead of using a string. A string would be converted to a function. This function will be executed within the scope of the window.

  setInterval("slide()", 3000);
//becomes
  setInterval(Function("slide();"), 3000);
三生路 2024-12-18 21:02:31

是的,setTimeout 只运行一次。您需要setInterval。此函数还返回一个可用于取消间隔的 ID。例如:

const slideInterval = setInterval(slide, 3000);

// later...
clearInterval(slideInterval);

Yes, setTimeout only runs once. You want setInterval. This function also returns an ID you can use to cancel the interval. For example:

const slideInterval = setInterval(slide, 3000);

// later...
clearInterval(slideInterval);
隐诗 2024-12-18 21:02:31

setTimeout 函数仅运行一次!如果您想运行更多次,您应该使用 setInterval

var loop_handle= setInterval("slide()",'3000');

您也可以在 slide() 函数末尾使用 setTimeout 来再次重新设置超时:

var loop_handle;
function slide() {
    if($('.current').is(':last-child')) {
        $('.current').removeClass('.current');
        $('#imgholder').first().addClass('.current');
        $('#imgholder').animate({left: '3920px'});
    }
    else {
        $nxt=$(".current");
        $(".current").removeClass("current");
        $nxt.next().addClass("current");
        $('#imgholder').animate({left: '-=980'},{duration: 'slow', easing: 'easeOutBounce' });
    }
    loop_handle = setTimeout("slide()",'3000');
}
loop_handle = setTimeout("slide()",'3000');

The setTimeout function only runs once! If you want to run it in more times you should use setInterval:

var loop_handle= setInterval("slide()",'3000');

Also you can use setTimeout in the end of the slide() function to re-set-timeout again:

var loop_handle;
function slide() {
    if($('.current').is(':last-child')) {
        $('.current').removeClass('.current');
        $('#imgholder').first().addClass('.current');
        $('#imgholder').animate({left: '3920px'});
    }
    else {
        $nxt=$(".current");
        $(".current").removeClass("current");
        $nxt.next().addClass("current");
        $('#imgholder').animate({left: '-=980'},{duration: 'slow', easing: 'easeOutBounce' });
    }
    loop_handle = setTimeout("slide()",'3000');
}
loop_handle = setTimeout("slide()",'3000');
清醇 2024-12-18 21:02:31

您只调用它一次,因此它只会执行一次。

也许您正在考虑“setInterval()”。

顺便说一下,当你调用它时,只需传递函数的名称而不是字符串:

setInterval(slide, 3000);

You only call it once, so it'll only execute once.

Perhaps you're thinking of "setInterval()".

When you call it, by the way, just pass the name of the function and not a string:

setInterval(slide, 3000);
寄意 2024-12-18 21:02:31

将 setTimeout 与递归结合使用(无限循环)

如果要保持每个函数调用之间的精确间隔,请使用 setTimeout 而不是 setInterval。 setInterval 可能会在某个时刻重叠,这通常不是预期的行为。这样你也可以和 couter 和 stopit if xondi

(function test(){
  setTimeout(function(){
    console.log(1);
    test();
  }, 2000)
})()

或有条件地使用它;

// Declare recursive function
function test(runCount, runMax, waitBeforeRun){
  // ... do sometnig here
  console.log("runCount " + runCount);
  console.log("waitBeforeRun " + waitBeforeRun);

  // adjust your varibles in loop
  runCount++;
  let reduceWaitRunTimeBy = 99;
  waitBeforeRun = 0 > waitBeforeRun - reduceWaitRunTimeBy ? waitBeforeRun : waitBeforeRun -= reduceWaitRunTimeBy;
   
 /** Run recursion 
  *  if "if" condition will not make a return
  **/
  if(runCount > runMax) return;
     
  setTimeout(test.bind(null, runCount, runMax, waitBeforeRun), waitBeforeRun);
  
}

// Run Timeout
let runCount = 0;
let runMax = 30;
let waitBeforeRun = 2000;
setTimeout(test.bind(null, runCount, runMax, waitBeforeRun), waitBeforeRun);

使用我的代码笔此处尝试一下,

这将在您的控制台中输出:

"runCount 0"
"waitBeforeRun 2000"
"runCount 1"
"waitBeforeRun 1901"
"runCount 2"
"waitBeforeRun 1802"
"runCount 3"
"waitBeforeRun 1703"
"runCount 4"
"waitBeforeRun 1604"
"runCount 5"
"waitBeforeRun 1505"
"runCount 6"
"waitBeforeRun 1406"
"runCount 7"
"waitBeforeRun 1307"
"runCount 8"
"waitBeforeRun 1208"
"runCount 9"
"waitBeforeRun 1109"
"runCount 10"
"waitBeforeRun 1010"
"runCount 11"
"waitBeforeRun 911"
"runCount 12"
"waitBeforeRun 812"
"runCount 13"
"waitBeforeRun 713"
"runCount 14"
"waitBeforeRun 614"
"runCount 15"
"waitBeforeRun 515"
"runCount 16"
"waitBeforeRun 416"
"runCount 17"
"waitBeforeRun 317"
"runCount 18"
"waitBeforeRun 218"
"runCount 19"
"waitBeforeRun 119"
"runCount 20"
"waitBeforeRun 20"
"runCount 21"
"waitBeforeRun 20"
"runCount 22"
"waitBeforeRun 20"
"runCount 23"
"waitBeforeRun 20"
"runCount 24"
"waitBeforeRun 20"
"runCount 25"
"waitBeforeRun 20"
"runCount 26"
"waitBeforeRun 20"
"runCount 27"
"waitBeforeRun 20"
"runCount 28"
"waitBeforeRun 20"
"runCount 29"
"waitBeforeRun 20"
"runCount 30"
"waitBeforeRun 20"

或使用 OOP - 代码此处

use setTimeout with recursion (endless loop)

If you want to keep the exact space between each function call use setTimeout instead setInterval. setInterval can overlap at some point and this is often not expected behavior. This way you can also and couter and stopit if xondi

(function test(){
  setTimeout(function(){
    console.log(1);
    test();
  }, 2000)
})()

Or use it with conditions;

// Declare recursive function
function test(runCount, runMax, waitBeforeRun){
  // ... do sometnig here
  console.log("runCount " + runCount);
  console.log("waitBeforeRun " + waitBeforeRun);

  // adjust your varibles in loop
  runCount++;
  let reduceWaitRunTimeBy = 99;
  waitBeforeRun = 0 > waitBeforeRun - reduceWaitRunTimeBy ? waitBeforeRun : waitBeforeRun -= reduceWaitRunTimeBy;
   
 /** Run recursion 
  *  if "if" condition will not make a return
  **/
  if(runCount > runMax) return;
     
  setTimeout(test.bind(null, runCount, runMax, waitBeforeRun), waitBeforeRun);
  
}

// Run Timeout
let runCount = 0;
let runMax = 30;
let waitBeforeRun = 2000;
setTimeout(test.bind(null, runCount, runMax, waitBeforeRun), waitBeforeRun);

Try it with my Code Pen here

Which would output in your console this:

"runCount 0"
"waitBeforeRun 2000"
"runCount 1"
"waitBeforeRun 1901"
"runCount 2"
"waitBeforeRun 1802"
"runCount 3"
"waitBeforeRun 1703"
"runCount 4"
"waitBeforeRun 1604"
"runCount 5"
"waitBeforeRun 1505"
"runCount 6"
"waitBeforeRun 1406"
"runCount 7"
"waitBeforeRun 1307"
"runCount 8"
"waitBeforeRun 1208"
"runCount 9"
"waitBeforeRun 1109"
"runCount 10"
"waitBeforeRun 1010"
"runCount 11"
"waitBeforeRun 911"
"runCount 12"
"waitBeforeRun 812"
"runCount 13"
"waitBeforeRun 713"
"runCount 14"
"waitBeforeRun 614"
"runCount 15"
"waitBeforeRun 515"
"runCount 16"
"waitBeforeRun 416"
"runCount 17"
"waitBeforeRun 317"
"runCount 18"
"waitBeforeRun 218"
"runCount 19"
"waitBeforeRun 119"
"runCount 20"
"waitBeforeRun 20"
"runCount 21"
"waitBeforeRun 20"
"runCount 22"
"waitBeforeRun 20"
"runCount 23"
"waitBeforeRun 20"
"runCount 24"
"waitBeforeRun 20"
"runCount 25"
"waitBeforeRun 20"
"runCount 26"
"waitBeforeRun 20"
"runCount 27"
"waitBeforeRun 20"
"runCount 28"
"waitBeforeRun 20"
"runCount 29"
"waitBeforeRun 20"
"runCount 30"
"waitBeforeRun 20"

Or use OOP - Code Pen here

朱染 2024-12-18 21:02:31

这是因为 setTimeout() 应该只运行一次。为了按设定的时间间隔触发事件,请使用 setInterval()

That's because setTimeout() is supposed to run only once. In order to fire an event on set intervals user setInterval().

悲喜皆因你 2024-12-18 21:02:31

当您在递归循环中使用 setTimeout 时,通常会发生此问题,例如在 ajax 回调中,当您想要在递归之外运行某些内容时,您希望 setTimeout 能够像过去一样工作。记住在非递归函数中使用 setInterval。

This problem usually happens when you used setTimeout in recursion loop, for example in ajax callback and when you want to run something out of recursion, you expect setTimeout to work as past. remember to use setInterval in non-recursion functions.

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