简体   繁体   English

如何结束用于调用 function 的间隔,该间隔使用 Howler.js 更新时间

[英]How to end interval used to call function which updates time with Howler.js

I have made a function UpdateTime(x) which updates gets the current and total time of a song and writes in inside a div .我制作了一个 function UpdateTime UpdateTime(x) ,它更新获取歌曲的当前时间和总时间并写入div

I call this function on onload ( Howler.js ), so it writes 0:0/song:time when the song is loaded and on onplay ( Howler.js ) with an interval every 50 miliseconds so it automatically updates the time when I start playing the song.我在onload ( Howler.js ) 上调用此 function ,因此它会在歌曲加载和onplay ( Howler.js ) 时写入0:0/song:time ,每 50 毫秒间隔一次,因此它会自动更新我开始的时间播放歌曲。

My problem is that this will cause an endless loop and effect performance.我的问题是这会导致无限循环并影响性能。

My question is: How do I end that endless loop when the song stops playing?我的问题是:当歌曲停止播放时,我如何结束这个无限循环?

If any of you have a more elegant approach to updating the time with Howler.js I would also appreciate it.如果你们中的任何人有更优雅的方法来使用Howler.js更新时间,我也将不胜感激。

var songname = new Howl({
    src: ['mp3/songname.mp3'],
    onload: function() {
        UpdateTime(songname);
    },
    onplay: function() {
        setInterval(function(){
            UpdateTime(songname);
        },50);
    }/*,
    onend: function() {
        maybe end the interval here?...
    }*/
});

function UpdateTime(x){
    let a = (x.seek()).toFixed();
    let a2 = Math.floor(a / 60);
    let a1 = a - a2 * 60;
    let b = (x.duration()).toFixed();
    let b2 = Math.floor(b / 60);
    let b1 = b - b2 * 60;
    document.getElementById("time").innerHTML=a2+":"+a1+" / "+b2+":"+b1;
}

This is a bug, but not surly the solution of your issue:这是一个错误,但不是您问题的解决方案:

Here you assign a string to b ( toFixed() returns a string):在这里,您将一个字符串分配给 b ( toFixed()返回一个字符串):

let b = (x.duration()).toFixed();

And here you try to divide a string by 60.在这里,您尝试将字符串除以 60。

let b2 = Math.floor(b / 60);

The result is undefined .结果是undefined的。

var durationUpdater;

var song = new Howl({
    // song is still missing bunch of stuff here, but irrelevant for this issue

    onplay: function() {
        durationUpdater = setInterval(function(){
            UpdateT1();
            console.log(currentsong + ': song-progress updated');
        },500);
    },
    onend: function() {
        clearInterval(durationUpdater);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM