如何减少重复的javascript代码

发布于 2024-11-17 07:59:28 字数 546 浏览 2 评论 0原文

无论如何我可以减少下面的重复吗?我只显示了两个代码块,但有并将有更多相同的代码块。

我尝试过使用数组和循环,但不幸的是我无法获得有效的示例。先感谢您。

E1 = new Audio('audio/E1.ogg');
E1.addEventListener('ended', function() {
  this.currentTime = 0;
  this.play();
}, false);

A1 = new Audio('audio/A1.ogg');
A1.addEventListener('ended', function() {
  this.currentTime = 0;
  this.play();
}, false);

编辑:使用下面的乔纳森的代码,我仍然想知道是否可以做相当于:

(E1,A1,x,x,x).addEventListener('ended', callback, false);
// I know this bit of code doesn't work

Is there anyway I can reduce the repetition below? I have only shown two code blocks but there are and will be many more of the same.

I have tried using arrays and loops, but unfortunately I could not get a working example. Thank you in advance.

E1 = new Audio('audio/E1.ogg');
E1.addEventListener('ended', function() {
  this.currentTime = 0;
  this.play();
}, false);

A1 = new Audio('audio/A1.ogg');
A1.addEventListener('ended', function() {
  this.currentTime = 0;
  this.play();
}, false);

EDIT : Using Jonathan's code below, I am still wondering whether it would be possible to do the equivalent of:

(E1,A1,x,x,x).addEventListener('ended', callback, false);
// I know this bit of code doesn't work

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

别再吹冷风 2024-11-24 07:59:28

由于您的回调是相同的,您可以将它们绑定到一个变量:

var E1 = new Audio('audio/E1.ogg');
var A1 = new Audio('audio/A1.ogg');

var callback = function() {
    this.currentTime = 0;
    this.play();
};

E1.addEventListener('ended', callback, false);
A1.addEventListener('ended', callback, false);

Since your callbacks are the same you can just bind them to a variable:

var E1 = new Audio('audio/E1.ogg');
var A1 = new Audio('audio/A1.ogg');

var callback = function() {
    this.currentTime = 0;
    this.play();
};

E1.addEventListener('ended', callback, false);
A1.addEventListener('ended', callback, false);
寄离 2024-11-24 07:59:28
var files = ['audio/E1.ogg', 'audio/A1.ogg'];
//note that we cannot/should not use for(... in ...) - that won't do what you expect
for(var i = 0; i < files.length; ++i) {
    var audio = new Audio(files[i]);
    audio.addEventListener('ended', function() {
        this.currentTime = 0;
        this.play();
    }, false);
}
var files = ['audio/E1.ogg', 'audio/A1.ogg'];
//note that we cannot/should not use for(... in ...) - that won't do what you expect
for(var i = 0; i < files.length; ++i) {
    var audio = new Audio(files[i]);
    audio.addEventListener('ended', function() {
        this.currentTime = 0;
        this.play();
    }, false);
}
温暖的光 2024-11-24 07:59:28

类似的东西

var addEndedEvent = function(elem) {
    elem.addEventListener('ended', function() {
        this.currentTime = 0;
        this.play();
    }, false);
}

addEndedEvent(new Audio('audio/A1.ogg'));

something like

var addEndedEvent = function(elem) {
    elem.addEventListener('ended', function() {
        this.currentTime = 0;
        this.play();
    }, false);
}

addEndedEvent(new Audio('audio/A1.ogg'));
原野 2024-11-24 07:59:28

你应该能够做这样的事情。如果您有更多文件,只需将它们的名称添加到数组 fileNames 中即可。

var audioRefs = { }, fileNames = ['E1','A1','B1'], i, file;
for( i = 0; i < fileNames.length; i++ ) {
    file = fileNames[i];
    audioRefs[file] = new Audio('audio/' + file + '.ogg');
    audioRefs[file].addEventListener('ended', callback, false);
}

function callback() {
   this.currentTime = 0;
   this.play();
};

audioRefs 最终看起来像......

audioRefs = {
   'A1': (reference to A1 Audio object),
   'B1': (reference to B1 Audio Object)
}

You should be able to do something like this. If you have more files, just add their name into the array fileNames.

var audioRefs = { }, fileNames = ['E1','A1','B1'], i, file;
for( i = 0; i < fileNames.length; i++ ) {
    file = fileNames[i];
    audioRefs[file] = new Audio('audio/' + file + '.ogg');
    audioRefs[file].addEventListener('ended', callback, false);
}

function callback() {
   this.currentTime = 0;
   this.play();
};

audioRefs will end up looking like....

audioRefs = {
   'A1': (reference to A1 Audio object),
   'B1': (reference to B1 Audio Object)
}
好多鱼好多余 2024-11-24 07:59:28

对于编辑后的问题,如果您使用 Underscore.jseach 函数a> 您可以执行以下操作:

_.each([E1,A1,B1], function(audio) { 
    audio.addEventListener('ended', callback, false); 
});

For the edited question, if you use the each function from Underscore.js you can do the following:

_.each([E1,A1,B1], function(audio) { 
    audio.addEventListener('ended', callback, false); 
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文