gpt4 book ai didi

javascript - 如何在 CoffeeScript 上创建音频 onending 事件

转载 作者:行者123 更新时间:2023-12-03 04:35:20 25 4
gpt4 key购买 nike

当音频以 CoffeeScript 结束时,我试图调用一个函数,我在 JavaScript 中发送了代码,它工作得很好,但在 CoffeeScript 中却不行。

这是coffescript上的完整代码。

play: () ->
if (@getConfig "musicPath") != "../audioclips/backgroundmusics/"
@pathtoMusic = @getConfig "musicPath"
else
@pathtoMusic = path.join(__dirname, @getConfig "musicPath")

@musicFiles = fs.readdirSync(@pathtoMusic.toString())
@music = new Audio(@pathtoMusic + @musicFiles[0])
@music.volume = @getConfig "musicVolume"

@isPlaying = false if (@music.paused)
return null if @isPlaying

@isPlaying = true
@music.play()
@music.onended = ->
@music.play() //-----Here doesn't work------//

我正在使用atom文本编辑器来朗读包中的coffeescript代码,并使用chrome来朗读javascript代码。

更具体一些。我看到一个包在atom上使用onending事件,这是代码。

code-champion

'use babel';

epicVictory() {
if (this.isPlaying) return
console.log('Epic Victory!');
let maxIndex = this.winFiles.length - 1
let minIndex = 0
let randomIndex = Math.floor(Math.random() * (maxIndex - minIndex + 1) + minIndex)
this.audio = new Audio(this.winpath + this.winFiles[randomIndex])

this.audio.onended = () => {
this.audio.play() //-------here work-----//
//this.isPlaying = false
}

this.isPlaying = true

this.audio.volume = this.volume
this.audio.play()
return true
},

我使用在线转换器将 babel 代码转换为 javascript,我得到了这个。

babel to javascript

//imput
this.music.onended = () => {
this.audio.play()
}

//ouput
this.music.onended = function () {
this.music.play();
};

然后我将 javascript 代码转换为 CoffeeScript 编译器,我得到了这个。

javascript to coffeescript

//imput
this.music.onended = function () {
this.autoPlay();
}

//ouput
@music.onended = ->
@music.play()

但是该代码仅适用于 babel。我不知道我做错了什么。

最佳答案

您可能想使用coffescript中的粗箭头函数来绑定(bind)您的“this”:

@music.onended = =>
@autoPlay()

如果您可以发布更多上下文,可能会有所帮助。

关于javascript - 如何在 CoffeeScript 上创建音频 onending 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43329098/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com