2

编辑:我的意思是多次触发,newjob() 将触发 3 次,每 5 秒...所以在 20 秒内我将触发 12 次,而不是我想要的 4 次。所以它每 5 秒触发多次,而不是每 5 秒触发一次。

我有一个使用 Toastr 创建的函数,用于在我的 Web 应用程序上显示一条消息。我最终会将它与 API 的 ajax 请求联系起来,以确定是否显示消息,但现在我只是在测试它的外观。

我正在设置一个间隔,但它会多次触发其中的函数(通常是 3 次)。

$(document).ready(function() {
    setInterval(function () {
        newJob();
    }, 5000);
});

我不能做 setInterval(function(e) { } 因为 e 是未定义的,因为没有与之关联的事件,点击时,我使用 e.stopImmediatePropagation(); 让它只触发一次。我怎么能如果我没有 e,请在设定的时间间隔内停止这种立即传播?

谢谢你。

编辑:完整代码:

var newJob = function(e) {
    var i = -1;
    var $toastlast;

    var getMessage = function () {
        var msgs = ["There's a new job in the job dispatch queue", "A job pending approval has timed out"];
        i++;
        if (i === msgs.length) {
            i = 0;
        }

        return msgs[i];
    };

    var shortCutFunction = "success"; // 'success' or 'error'

    toastr.options = {
        closeButton: true,
        progressBar: true,
        debug: false,
        positionClass: 'toast-top-full-width',
        onclick: null,
        timeOut: "0",
        extendedTimeOut: "0",
        showDuration: "0",
        hideDuration: "0",
        showEasing: "swing",
        hideEasing: "linear",
        showMethod: "fadeIn",
        hideMethod: "fadeOut",
    };


    toastr.options.onclick = function () {
        window.location.href = "/dispatcher";
    };

    var msg = getMessage();

    $("#toastrOptions").text("Command: toastr["
                    + shortCutFunction
                    + "](\""
                    + msg
                    + "\")\n\ntoastr.options = "
                    + JSON.stringify(toastr.options, null, 2)
    );

    var $toast = toastr[shortCutFunction](msg);

};

$(document).ready(function() {
    setInterval(function () {
        console.log('set interval');
        newJob();
    }, 5000);


});

这是我的 index.phtml 文件:

    <?php
echo $this->headScript()->appendFile($this->basePath() . '/plugins/toastr/toastr.js')
echo $this->headScript()->appendFile($this->basePath().'/js/dispatchernotification.js');
    ?>

我所做的只是将我想要运行的 javascript 添加到我的 index.phtml 文件和 toastr 库中。通过 console.loging 在间隔内,我得到三个日志。

这是一个小提琴..不确定如何运行它,因为它已经准备好了 http://jsfiddle.net/efecarranza/rfvbhr1o/

4

5 回答 5

3

setInterval将无休止地继续下去。我想你正在寻找setTimeout.

于 2015-05-27T15:55:09.390 回答
2

setInterval: 每 x 毫秒重复调用一个函数
setTimeOut: x 毫秒后调用一个函数。

你有两个选择:

不再需要时清除间隔:

var intervalId;
$(document).ready(function() {
    intervalId = setInterval(function () {
        newJob();
    }, 5000);
});

// At some other point
clearInterval(intervalId);

或者,在您的情况下,更简单的解决方案是使用 setTimeout:

$(document).ready(function() {
        setTimeout(function () {
            newJob();
        }, 5000);
    });
于 2015-05-27T16:02:48.173 回答
2

确保把你的setInterval外面$(document).ready(function() {...})。所以它会像:

<script>

$(document).ready(function() {
    ... some code ...
});

var myInterval;
clearInterval(myInterval);
myInterval = setInterval(function() {
    ... your code is here ...
}, 5000);

</script>

出于某种原因,如果$(document).ready()每次您再次动态地打开同一页面时,它会双重设置setInterval进度,并且clearInterval在您实际刷新浏览器之前,该功能无济于事。

于 2017-10-15T20:45:35.353 回答
0

如果您可以在代码中添加一个 jsfiddle 会有所帮助,这样我们就可以准确地看到出了什么问题。无论出于何种原因,都可能多次调用 setInterval 函数。尝试在该函数的开头添加一个 console.log 语句来检查是否是这种情况。

如果是这样并且您无法弄清楚原因(我们也不知道您的代码),您可以在函数的开头进行检查,如下所示:

if (typeof this.installInterval.done == "undefined") {
  /*a bunch of code*/
  this.installInterval.done = true
}
于 2015-05-27T16:51:22.410 回答
0

可能是错误的,您像我一样两次调用您的脚本!在下面的代码中,我两次调用了“testLoop.js”。

<script src="../js/testLoop.js"></script>
<script type="text/javascript" src="../js/testLoop.js"></script>
<script type="text/javascript" src="../js/cube.js"></script> 
于 2020-02-27T20:40:11.153 回答