使用Jquery ajax后台声音提醒订单信息
为了实现这个需求,我们可以使用 jQuery 的 AJAX 功能来请求服务器,并使用 CSS 和 JavaScript 来创建一个右下角弹出的消息框,并在显示消息框的同时播放一个声音提醒。以下是一个详细的实现步骤:
HTML: 创建一个简单的 HTML 结构,并添加一个隐藏的用于显示消息的 div。
<!DOCTYPE html>
<html lang="en">
<head>
<title>使用Jquery ajax后台声音提醒订单信息</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Message Notification</title>
<style>
.message-box {
position: fixed;
right: 20px;
bottom: 20px;
width: 200px;
padding: 10px;
background-color: #333;
color: #fff;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
opacity: 0;
transform: translateY(20px);
transition: opacity 0.3s, transform 0.3s;
z-index: 9999;
}
.message-box.show {
opacity: 1;
transform: translateY(0);
}
<!--CSS: 样式化消息框,使其显示在右下角,并添加动画效果。--!>
</style>
</head>
<body>
<div id="messageBox" class="message-box">
<span id="messageContent"></span>
</div>
<audio id="alertSound" src="alert.mp3" autoplay="false"></audio>
<script src="/uploads/allimg/241202/225Q91408-1.jpg"></script>
<!--JavaScript (jQuery): 使用 AJAX 请求服务器,处理返回的数据,并在有消息时显示消息框和播放声音。--!>
<script>
$(document).ready(function() {
function checkMessages() {
$.ajax({
url: '你的请求订单地址信息',
method: 'GET',
dataType: 'json',//返回json数据 包括code状态 和count需提醒的条数
success: function(response) {
if (response.code === 1 && response.count > 0) {
//如果有数据触发后台声音提醒订单信息
showMessage(response.count);
}
},
error: function() {
//请求可能发 生错误
console.error('Error fetching messages');
}
});
}
function showMessage(count) {
var messageBox = $('#messageBox');
var messageContent = $('#messageContent');
var alertSound = $('#alertSound')[0];
messageContent.text('有【' + count + '】条信息需要处理');
messageBox.addClass('show');
// 可以播放提醒音频
alertSound.play();
// Remove message after 5 seconds
setTimeout(function() {
messageBox.removeClass('show');
setTimeout(function() {
messageContent.text('');
}, 300); // Ensure the text is cleared after the fade-out animation
}, 5000);//多少秒更新一次
}
// Check for messages every 10 seconds
setInterval(checkMessages, 3000);//每隔3秒触发请求一次AJAX获得最新的提醒数量
// Initial check
checkMessages();//第一次加载AJAX获取提醒数据
});
</script>
</body>
</html>