javascript - playing audio in ajax callback -
i create jquery's code auto refresh specific div. in auto refresh using ajax create notification if there have new request client, social network notif. use music notif. when new request comes, music on. before music finished, next notif comes, , music on too. so, there 2 music playing , looked bug.
i want play 1 music, next or on come. should do? should check in success ajax if music played?
function refresh() { var ini; var requestmasuk = $('#request_belum_terima').text(); var audioelement = document.createelement('audio'); audioelement.setattribute('src', '<?php echo base_url() . 'assets/music/noty.mp3'; ?>'); audioelement.addeventlistener('ended', function() { this.currenttime = 0; this.play(); }, false); var myaudio = document.getelementsbytagname('audio'); settimeout(function() { $.ajax({ url: '<?php echo base_url() . 'control_closing/hitungrequestbelumterima/' ?>', type: 'post', datatype: 'json', success: function(obj) { if (obj > requestmasuk) { ini = obj; $('#request_belum_terima').text(obj); ========== failed ========================== if (myaudio.duration > 0 && !myaudio.paused) { alert('another request come'); } else { audioelement.play(); } ================================================= alert("new request come"); $('#things_table').fadeout('slow').load('<?php echo base_url() . 'monitoring/ajax_get_things_table' ?>').fadein('slow'); } refresh(); } }).always(function() { $('#request_belum_terima').text(ini); }).done(function() { $('#request_belum_terima').text(ini); }); }, 20000); $('#request_belum_terima').text(ini); }
the variable myaudio returns array. later on check !myaudio.paused can't work expects single audio element - not array.
if it's time same mp3 recommend use single audio element , not create every time new one. check element , play if it's paused.
Comments
Post a Comment