⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 commentchecker.js

📁 图灵Ajax三剑客:《Ajax基础教程》、《Ajax实战》、《Ajax高级编程》的源代码
💻 JS
字号:

var oXmlHttp = null;
var iInterval = 1000;
var iLastCommentId = -1;
var divNotification = null;
var blnRequestsEnabled = true;

function checkComments() {

    if (blnRequestsEnabled) {
        try {
            if (!oXmlHttp) {
                oXmlHttp = zXmlHttp.createRequest();
            } else if (oXmlHttp.readyState != 0) {
                oXmlHttp.abort();
            }    
            
            oXmlHttp.open("get", "CheckComments.php", true);
            oXmlHttp.onreadystatechange = function () {               
                
                if (oXmlHttp.readyState == 4) {
                    if (oXmlHttp.status == 200) {
        
                        var aData = oXmlHttp.responseText.split("||");
                        if (aData[0] != iLastCommentId) {                   
                            
                            if (iLastCommentId != -1) {                        
                                showNotification(aData[1], aData[2]);
                            }
                            
                            iLastCommentId = aData[0];
                        }
                        
                        setTimeout(checkComments, iInterval);             
                    } else {
                        throw new Error("An error occurred while making the request.");
                    }                        
                } 
            };    
        
            oXmlHttp.send(null); 
        } catch (oException) {
            blnRequestsEnabled = false;
        }
    } //End: if     
}

function showNotification(sName, sMessage) {
    if (!divNotification) {
        divNotification = document.createElement("div");
        divNotification.className = "notification";
        document.body.appendChild(divNotification);
    }
    
    divNotification.innerHTML = "<strong>New Comment</strong><br />" + sName 
              + " says: " + sMessage + "...<br /><a href=\"ViewComment.php?id=" 
              + iLastCommentId + "\">View</a>";
    divNotification.style.top = document.body.scrollTop + "px";
    divNotification.style.left = document.body.scrollLeft + "px";
    divNotification.style.display = "block";
    setTimeout(function () {
        divNotification.style.display = "none";
    }, 5000);
}

//if Ajax is enabled, assign event handlers and begin fetching
window.onload = function () {
    if (zXmlHttp.isSupported()) {
        checkComments();              
    }
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -