前台实时获取数据

实现网站实时通信

· 四种方式,第二三四种方式需要服务器端做操作的

  1. 写计时器进行Ajax轮询

    // 每秒调用一次http请求
    setInterval(function () {
         
       	var xhr = new XMLHttpRequest();
        xhr.open('POST', 'url', true);
        xhr.setRequestHeader('Content-Type', 'application/json');
        xhr.send();
        xhr.onreadystatechange = function () {
         
            if (xhr.readyState == 4 && xhr.status == 200) {
         
                console.log

你可能感兴趣的:(前端,ajax,websocket,javascript,前端)