Emettre un son en js avec setTimeout
Bonjour à tous et à toute.
J'ai un petit script en ajax qui vérifie dans ma bdd si j'ai une nouvelle commande toute les 20 secondes.
Si il y a une nouvelle commande (champ de table à 1) un bouton avec un lien vers mes commandes s'affiche.
J'aimerai aussi qu'un son retentisse pour éviter de devoir rester planté devant l'écran en attendant les commandes.
J'ai un petit script en js qui émet un son et j'aimerai le mettre dans la fonction ajax qui affiche le div mais je n'y arrive pas.
Je met mon script actuelle qui affiche la div et mon script js qui émet le son en espérant que quelqu'un puise m'aider.
Ajax qui demande à la page charger.php de vérifier si il y a une nouvelle commande.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| function writeInDiv(text){
var objet = document.getElementById('div_nbr_commande');
objet.innerHTML = text;
}
function ajax_verif_commande()
{
var xhr_commande = null;
if (window.XMLHttpRequest || window.ActiveXObject) {
if (window.ActiveXObject) {
try {
xhr_commande = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
xhr_commande = new ActiveXObject("Microsoft.XMLHTTP");
}
} else {
xhr_commande = new XMLHttpRequest();
}
} else {
alert("Votre navigateur ne supporte pas l'objet XMLHTTPRequest...");
return null;
}
xhr_commande.open("GET", "http://mon-site/insert/charger.php", false);
xhr_commande.send(null);
writeInDiv(xhr_commande.responseText);
setTimeout("ajax_verif_commande()", 20000);
} |
page charger.php qui va interroger la bdd pour voir si il y a une nouvelle commande.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <?php
header ("Cache-Control: no-cache");
include('../includes/identifiants.php');
$req_news_com = $bdd->prepare('SELECT new_com FROM commandes WHERE new_com = :nouv_com');
$req_news_com->bindValue('nouv_com', '0', PDO::PARAM_INT);
try
{
$req_news_com->execute();
$total_req_news_com = $req_news_com->rowCount();
}
catch (PDOException $e)
{
echo 'Error : ' . $e->getMessage();
die();
}
if ($total_req_news_com > 0)
{
echo'<div class="tabs_rouge">';
echo'<a href="/admin/admin.php?page=aff_com" title="Il y a une nouvelle commande"><span>New Commande</span></a>';
echo'</div>';
}
$req_news_com->closeCursor();
?> |
Page haut.php ou le bouton s'affiche
Code:
1 2 3
| <?php
echo'<div id="div_nbr_commande"></div>';
?> |
et enfin mon script js qui émet un son.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <script type="text/javascript">
soundManager.setup(
{
url: '<?php echo ROOTPATH ; ?>/swf/',
onready: function()
{
var mySound = soundManager.createSound(
{
id: 'aSound',
url: '<?php echo ROOTPATH ; ?>/swf/ringtones.mp3'
});
mySound.play();
},ontimeout: function()
{}
});
</script><br><br> |
sans oublier
Code:
echo'<script type="text/javascript" src="'.ROOTPATH.'/js/soundmanager2.js"></script>';
Merci de votre aide.