码迷,mamicode.com
首页 > 其他好文 > 详细

循环执行n次的代码

时间:2016-09-28 01:36:20      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:

var audio = document.createElement("audio");  
var index = 0;
    audio.src = "piano/3C.mp3";  
    audio.addEventListener(‘ended‘, function () {  
    // Wait 500 milliseconds before next loop  
    setTimeout(function () {if(index<4){ audio.play(); index++}}, 500);  
    }, false);  
    audio.play();  

 

 

这里的index必须设置为全局变量,这是个闭包问题!
var audio = document.createElement("audio");  
var index = 0;
    audio.src = "piano/3C.mp3";  
    audio.addEventListener(‘ended‘, function () {  
    // Wait 500 milliseconds before next loop  
    setTimeout(function () {if(index<4){ audio.play(); index++}}, 500);  
    }, false);  
    audio.play();  

循环执行n次的代码

标签:

原文地址:http://www.cnblogs.com/yangmengsheng/p/5914755.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!