【问题标题】:Immediately stop a setTimeout loop without finishing current cycle立即停止 setTimeout 循环而不完成当前循环
【发布时间】:2014-12-31 19:18:14
【问题描述】:

我正在创建一个用户操作的随机选择器。用户点击一个按钮,系统会选择两个随机结果(将是视频)。

然后该页面将交替突出显示两个随机结果 - 用户可以再次点击该按钮,然后他们可以选择他们想要的视频。

这一切都很好,但是交替突出显示是通过一个 setTimeout 循环完成的,它完成了当前的循环,而不是立即停止。

我将如何获得它,以便当用户按下“选择”按钮时,flit 函数立即停止 - 而不是在当前循环结束时停止?

这是小提琴

http://jsfiddle.net/zandergrin/tmwst5z9/4/

ps - 抱歉,但这是一项正在进行中的工作 - 我知道这里有一些混乱的地方...... var 间隔 = 0;

function random() {
    // create the array
    var posts = ["post 1", "post 2", "post 3", "post 4", "post 5", "post 6", "post 7", "post 8", "post 9", "post 10"];
    var results = posts

    // Shuffle array
    .sort(function () {
        return .5 - Math.random()
    })

    // Get first 2 items
    .slice(0, 2);
    var post1 = results[0];
    var post2 = results[1];

    // add them to the DOM
    $('#res1').html(post1);
    $('#res2').html(post2);

    // loop it
    interval = setTimeout(random, 100);

}

function start() {
    // Hide the start button
    $("#start").css("display", "none");
    $("#stop").css("display", "block");
    // Start the randomizer
    random();

}


function stop() {
    // Hide the stop button and stop the random() timeout
    clearTimeout(interval);
    $("#stop").css("display", "none");
    $("#select").css("display", "block");

    // set the inital background colour
    $("#res1").css({'background': '#123','color': '#fff'});
    $("#res2").css({'background': '#eee','color': '#000'});

    //create a function to flick between items
    function flit() {
        //color the results
        setTimeout(function () {
            $("#res1").css({'background': '#eee','color': '#000'});
            $("#res2").css({'background': '#123','color': '#fff'});
        }, 800);

        //colour again after a delay
        setTimeout(function () {
            $("#res1").css({'background': '#123','color': '#fff'});
            $("#res2").css({'background': '#eee','color': '#000'});
        }, 1600);

        //loop it
        timer = setTimeout(arguments.callee, 1600);
    };
    //run the flick function
    flit();

}


function select() {
    // stop thie flit function - doesnt stop immediately!
    clearTimeout(timer);
    $("#select").css("display","none");
    $("#refresh").css("display", "block");
}

function refresh() {
    location.reload();
}

【问题讨论】:

  • 提示:按.5 - Math.random() 排序并不是洗牌数组的最佳方式,因为它允许a>b && b>c && c>a 为真,这会严重混淆sort 的实现。一个简单的随机播放是.map(function(x){return [Math.Random(),x]; }).sort(function(a,b){return a[0]-b[0];}).map(function(x){ return x[1];})。为了提高效率,谷歌 Knuth Fisher-Yates shuffle。
  • 谢谢你——我读过关于 Knuth Fisher-Yates shuffle 的文章。我理解其中的原因,但老实说,我用谷歌搜索的大多数实现都让我有点不知所措!现在使用它...
  • 要明确一点,该代码不是 Knuth-Fisher-Yates,它只是一个安全、无偏见的 shuffle,但不如 KFY 高效。

标签: javascript settimeout


【解决方案1】:

除了@sholanozie 的回答,我建议将那些 setTimeout 放在一个数组中(即arrayOfTimeouts),然后:

function select() {
    // stop thie flit function - doesnt stop immediately!
    arrayOfTimeouts.forEach(function(setTimer) {
        clearTimeout(setTimer);
});

【讨论】:

  • 不幸的是,这对我不起作用。不过还是谢谢。
【解决方案2】:

您的问题出在 flit() 函数中 - 您需要分配对 setTimeout 调用的引用,以便您可以对它们调用 clearTimeout。现在,您正在阻止 flit()select() 方法中被调用,但是这两个计时器仍在排队等待将来执行。

【讨论】:

猜你喜欢
  • 2012-01-16
  • 2015-03-25
  • 2017-08-27
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多