Javascript Array播放声音

时间:2018-05-07 02:16:53

标签: javascript jquery arrays

尝试解决此问题。我有这个" Users_Data> Heard_Sound"。我希望它执行一个函数" play_sound"如果Heard_Sound在任何这个数组中等于no。我希望它只执行一次。不是每个人都有一个" Heard_Sound"等于不喜欢它现在

{
     User_name: "Jon Doe",
     Mail_Id: "luiokiikukijuytjrytj",
     Role: "Cfo",
     Status: "Offline",
     Mail_Id: "2441784grt4g89rtg498r178",
     photo: "http://www.kktv.com/content/news/Help-authorities-identify-this-John-Doe-in-El-Paso-County-405067576.html",
     Heard_Sound: "No"
}

如果我有50个以上代码,它会同时为每个代码播放声音,使其非常响亮

////
    function play_sound(Heard_Sound){
        if(Heard_Sound == 'Noo'){
            let Audio_File = new 
            Audio("Assets/Audio/Alert_Sound/Alert_01.mp3");
            setTimeout(function(){
                Audio_File.play();
            }, 2000);
        }   
    }
////


$(document).ready(function(){
        let Users_Data = [
         {
                User_name: "Jon Doe",
                Mail_Id: "luiokiikukijuytjrytj",
                Role: "Cfo",
                Status: "Offline",
                Mail_Id: "2441784grt4g89rtg498r178",
                photo: "http://www.kktv.com/content/news/Help-authorities-identify-this-John-Doe-in-El-Paso-County-405067576.html",
                Heard_Sound: "No"
              },{
                User_name: "Jon Doe",
                Mail_Id: "luiokiikukijuytjrytj",
                Role: "Cfo",
                Status: "Offline",
                Mail_Id: "2441784grt4g89rtg498r178",
                photo: "http://www.kktv.com/content/news/Help-authorities-identify-this-John-Doe-in-El-Paso-County-405067576.html",
                Heard_Sound: "No"
              },{
                User_name: "Jon Doe",
                Mail_Id: "luiokiikukijuytjrytj",
                Role: "Cfo",
                Status: "Offline",
                Mail_Id: "2441784grt4g89rtg498r178",
                photo: "http://www.kktv.com/content/news/Help-authorities-identify-this-John-Doe-in-El-Paso-County-405067576.html",
                Heard_Sound: "No"
              },{
                User_name: "Jon Doe",
                Mail_Id: "luiokiikukijuytjrytj",
                Role: "Cfo",
                Status: "Offline",
                Mail_Id: "2441784grt4g89rtg498r178",
                photo: "http://www.kktv.com/content/news/Help-authorities-identify-this-John-Doe-in-El-Paso-County-405067576.html",
                Heard_Sound: "No"
              }
        ];


        function handle_Title(X){
            if(X  > 0){
                return `<div class="Page_Title text-align-center">
                <div class="Comminuity"><h1>Comminuty</h1></div>
                        <h4 class="Page_Title">(Found ${X} Users)</h4>
                    <hr />
                </div>
                `;
            }else{
                $( "#System_Alert" ).html( "<div class='alert alert-danger'>Error "+ X +" Users Found</div>" )
            }
        }
        function User_Position(User_Role){
            if(User_Role === 'Ceo'){
                return `<div class="position">${User_Role}</div>`;
            }else if(User_Role === 'President'){
                return `<div class="position">${User_Role}</div>`;
            }else if(User_Role === 'Cfo'){
                return `<div class="position">${User_Role}</div>`;
            }   
        }
        function User_Status(Status){
            if(Status == "Online"){
                return `<div class="Status" Style="color: limegreen;">${Status}</div>`;
            }else if(Status == "Offline"){
                return `<div class="Status" style="color: tomato;">${Status}</div>`;
            }else{
                return `<div class="Status" style="color: tomato;">Unavabile</div>`;
            }

        }
        function play_sound(Heard_Sound){
            if(Heard_Sound == 'Noo'){
                let Audio_File = new Audio("Assets/Audio/Alert_Sound/Alert_01.mp3");
                setTimeout(function(){
                    Audio_File.play();
                }, 2000);
            }   
        }
        function User_Img(img){
            return `<center><img src="${img}" id="COMM" class="User_Img box-shadow" style="max-height: 200px;     cursor: pointer;" data-toggle="modal" data-target="#VIEW_PICTURE"></center>`;
        }
        function User_Templete(User){
            `${play_sound(User.Heard_Sound)}`;
            return `
            <div class="col-lg-2">
                <div class="widget bg-black p-0">
                <div class="User_Name text-align-center p-10"><h3 class="m-t-0 m-b-10"style="color: white;">${User.User_name}</h3></div>
                <div class="USER_IMAGE">${User_Img(User.photo)}</div>
                <div class="p-15">
                <h4 class="INFO_STYLE text-align-center" style="color: white">
                    <div class="info">
                        <div class="Position">${User_Position(User.Role)}</div>
                        <div class="Status">${User_Status(User.Status)}</div>   
                    </div>
                </h4>
                    <div class="Action">
                    <button type="submit" id="MAIL" class="btn btn-primary btn-block" mailer_img="${User.photo}" data-toggle="modal" data-target="#Create_User" mail_id="${User.Mail_Id}">Mail</div><br>
                    <button type="submit" class="btn btn-primary btn-block" id="PROFILE" data-toggle="modal" data-target="#VIEW_USER" img="${User.photo}" mail_id="${User.Mail_Id}" user_name="${User.User_name}">View Profile</div>
                    </div>
                </div>
                </div>
            </div>  
            `;
        }



        function handle_Img_View(){
            $('img#COMM').click(function(){
            let Profile_Pic = $(this).attr('src');
                $('.VIEW_PICTURE').html('<div class=""><center><img style="max-height: -webkit-fill-available; resize: auto;" src="' + Profile_Pic  + '"></center></div>');

            });
        }
        function handle_View_Profile(){
            $('button#PROFILE').click(function(e){
                let PROFILE_IMG = $(this).attr('img');  
                let complete_img = '<center><img class="User_Img box-shadow" style="max-width: -webkit-fill-available; max-height: 300px" src="' + PROFILE_IMG  + '"></center>';
                $('.modal-body').html(complete_img);
            });
        }
        function handle_Message(){
            $('button#MAIL').click(function(e){
                let mail_Id = $(this).attr('mail_id');
                let Photo = $(this).attr('mailer_img');

                $('#Profile').html('<center><img class="User_Img box-shadow" style="max-height: 200px; ;" src="' + Photo + '"></center>');
                /*alert(mail_Id);*/
            });
        }
        function handle_Send_Message(){
            $('button#SEND').click(function(){
                alert('SENDING MESSAGE');
            });
        }

        document.getElementById("app").innerHTML = `
        ${Users_Data.map(User_Templete).join("")}`;

        handle_Message();
        handle_Send_Message();  
        handle_Img_View();  
        handle_View_Profile();  

});

1 个答案:

答案 0 :(得分:1)

您可以为此维护一个全局变量。 HEARD_ONCE

相应地更改play_sound功能

 let HEARD_ONCE = false; 

 function play_sound(Heard_Sound){
    if(Heard_Sound == 'Noo' && !HEARD_ONCE){
        HEARD_ONCE = true; 
        let Audio_File = new 
        Audio("Assets/Audio/Alert_Sound/Alert_01.mp3");
        setTimeout(function(){
            Audio_File.play();
        }, 2000);
    }   
}