【问题标题】:Angular js $timeout not working properly when i try to cancel and restart it当我尝试取消并重新启动它时,Angular js $timeout 无法正常工作
【发布时间】:2016-12-06 11:58:21
【问题描述】:

我正在尝试使用 Angular js $timeout 函数创建问题幻灯片,我能够实现它并且它工作得很好,但是当我尝试刷新幻灯片以便它可以重新开始时,刷新仅适用于第一张幻灯片,因为其他幻灯片不适用于超时延迟时间 下面是我的代码,main函数和recall函数。

    $scope.callTime = function() {
        q_len = questions.length;
            $timeout(function () {
                //checking if the question index is still valid
                if (q_indy < q_len) {
                    slides = questions[q_indy].pictures;
                    len = slides.length;
                    console.log(indy);
                    $scope.currentQuestion = questions[q_indy];
                    f_time = parseInt(slides[indy].time_frame);
                    //getting the specific time needed to run this particular slide
                    r_time = f_time - initialTime;
                    if (indy < len) {
                        f_time = parseInt(slides[indy].time_frame);
                        var interval = slides[indy].picture_url;
                        indy++;
                        console.log(q_indy + " " + indy + " " + r_time);
                        //changing to the current picture so it can run for the given time
                        $scope.image = $scope.url.url + interval;
                        if (indy == len) {
                            //checking if the question's slides is out of index so the next question should be loaded
                            q_indy++;
                            indy = 0;
                            initialTime = f_time;
                            console.log(q_indy + " " + indy + " " + r_time);
                            $scope.callTime();
                        }
                        else {
                            //if the slide index is active the next slide should be loaded then
                            initialTime = f_time;
                            $scope.callTime();
                        }
                        //console.log($scope.url.url + interval);
                    }
                }
            }, r_time);
            //time();
        }
    $scope.refresh = function () {
        $timeout.cancel($scope.callTime);
        q_indy = 0;
        indy = 0;
        initialTime = 0;
        r_time = 0;
        len = 0;
        q_len = 0;
        $scope.callTime();
       var e = document.getElementById('myTune');
        //e.pause();
        e.currentTime = 0;
        //e.play;
    };

【问题讨论】:

    标签: javascript angularjs timeout


    【解决方案1】:

    您需要保存调用$timeout 时返回的值并将该值传递给$timeout.cancel()。相反,您传入的是用于创建超时的函数,$timeout.cancel() 对此一无所知。

    var timer = null;
    $scope.callTime = function() {
        q_len = questions.length;
        $timeout.cancel(timer); // You probably also want to cancel here
        timer = $timeout(function () {
              // ... rest of your code here ...
            }, r_time);
            //time();
        }
    $scope.refresh = function () {
        $timeout.cancel(timer);
        // ... and rest of your code here ...
    };
    

    【讨论】:

    • 非常感谢...这有效,我只将超时函数包装在计时器变量中,瞧它有效
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 2020-10-03
    相关资源
    最近更新 更多