首页 > 解决方案 > 如何设置通知声音和“橙色闪烁标签”

问题描述

我正在尝试向我的简单 php/js 聊天发出通知:

首先,我正在尝试比较我的 log.html(其中包含消息):

我原来的 post.php ( //jQuery 请求。每次用户提交表单并发送新消息时,它都会将客户端的输入和数据发送到 post.php 文件。) post.php 将消息保存到 log.html 文件中。

 <?php
 session_start();

 if(isset($_SESSION['name'])){
    $text = $_POST['text'];
    $text_message = "<div class='msgln'><span class='chat-time'>".date("F j, g:i A")."</span> <b class='user-name'>".$_SESSION['name']."</b> ".stripslashes(htmlspecialchars($text))."<br></div>";

    file_put_contents("log.html", $text_message, FILE_APPEND | LOCK_EX);
}
?>

我编辑的 post.php (试图设置一个函数,它正在检查“新”消息 - 与旧消息进行比较)。

<?php
session_start();
if(isset($_SESSION['name'])){
    $text = $_POST['text'];     
 
    $text_message = "<div class='msgln'><span class='chat-time'>".date("F j, g:i A")."</span> <b class='user-name'>".$_SESSION['name']."</b> ".stripslashes(htmlspecialchars($text))."<br></div>";

    // Check if content is different

    function text_check(){
        $ah = fopen("log.html", 'rb');
        $aha = preg_match(fread($ah, 8192), $text_message);
        $result = true;

        while(!feof($ah)) {
            if( $aha === false){
                $result = false;
                break;
            }
        }
        fclose($ah);
        return $result;    
    }

file_put_contents("log.html", $text_message, FILE_APPEND | LOCK_EX);    
}
?>

如果 post.php 函数“text_check()”在我的 index.php 中返回 true:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">

function checkUpdate()
{
    $.post("post.php", function(text_check){
        if (text_check.toString()=="true") {
            playSound();
        }
    });
}

function playSound()
{
    var audio = new Audio('my_audio');
    audio.play();
}

这是一个好的概念吗?

当浏览器最小化时,如何发出“橙红色闪烁”通知?

我读了一些关于标题的东西 - 必须改变它。如果我每秒都用 JS 更改它,它会起作用吗?

标签: javascriptphpjqueryajax

解决方案


推荐阅读