×÷Ϊһ¸öÀÏÇ°¶Ë£¬±¾°¸ÀýÊÇ»ùÓÚjqueryÀ´Ð´µÄ¡£
Ç°¶ËäÖȾҳÃæÄÃÊý¾Ý£¬Î޷ǾÍÊÇajax¡¢socket£¬ÆäËûµÄÔÝʱûÓÐÓùý£¬µ«ÏîÄ¿»¹ÊÇʹÓÃajax±È½Ï¶à¡£
ÏÂÃæÀ´¿´Ò»ÏÂÒ»¸ö¼òµ¥»ùÓÚajax¶ÌÂÖѯµÄÇëÇó
function req() { $.ajax({ type: 'get', url: 'demo.php', dataType: 'json', success: function(res) { console.log(res); }, error: function() { console.log('ÇëÇóʧ°Ü~'); } }); } req(); setInterval(req, 3000);Èç¹ûÍøËÙ¿ì¶øÎȶ¨µÄ»°£¬¿ÉÒÔÕâÑùʹÓ㬵«ÍøËÙËÄÜÈ·¶¨ÄØ£¬Èç¹ûÍøËÙ²»Îȶ¨µÄ»°£¬ÇëÇóÒ»¸ö½Ó¿ÚÐèÒª5~10Ã룬ÕâÑù¾Í»áÔì³ÉajaxÇëÇó¶Ñ»ý£¬½ü¶øÒý·¢²»¿É¹ÀÁ¿µÄÎÊÌ⣬ÄÇôÔõÑùÈ¥±ÜÃâÕâ¸öÎÊÌâÄØ£¿
·½Ê½Ò»£º¸øÇëÇó¸³ÉÏÒ»¸ö±äÁ¿£¬È»ºóÿ´ÎÂÖѯÏÈabortµôÉÏÒ»¸öÇëÇó
var ajaxReq = null; function req(isLoading) { if(ajaxReq !== null) { ajaxReq.abort(); ajaxReq = null; } ajaxReq = $.ajax({ type: 'get', url: 'demo.php', dataType: 'json', beforeSend: function() { if(isLoading) { $('.zh-loading').show(); } }, success: function(res) { console.log(res); }, complete: function() { if(isLoading) { $('.zh-loading').hide(); } }, error: function() { console.log('ÇëÇóʧ°Ü~'); } }); } req(true); setInterval(function() { req(false); }, 3000);ÃÍÒ»¿´£¬¸Ð¾õ»¹ÐУ¬²î²»¶à¾ÍOKÁË£¬µ«×÷Ϊǰ¶ËµÄÎÒÃÇÒª²»¶ÏµÄȥѰÕÒ¸üºÏÊʵķ½Ê½£¬ËùÒÔÓиöÏÂÃæÕâ¸ö¡£
·½Ê½¶þ£ºÃ¿Ò»´ÎÂÖѯ¶¼ÅжÏÉÏÒ»´ÎÇëÇóÊÇ·ñÍê³É£¬Íê³ÉÁ˲ŻáÖ´ÐÐÏÂÒ»´ÎµÄÇëÇó£¨ÍƼö£©
var isLoaded = false; function req(opts) { $.ajax({ type: 'get', url: 'demo.php', dataType: 'json', beforeSend: function() { if(opts.init === 1) { $('.zh-loading').show(); } isLoaded = false; }, success: function(res) { console.log(res); }, complete: function() { if(opts.init === 1) { $('.zh-loading').hide(); } isLoaded = true; }, error: function() { console.log('ÇëÇóʧ°Ü~'); } }); } req({"init": 1}); setInterval(function() { isLoaded && req({"init": 0}); }, 3000);ÉÏÃæµÄ isLoaded && req({"init": 0}); ±íʾǰÃæÒ»¸öÌõ¼þÕýÈ·£¬ÔòÖ´ÐÐ&&ºóÃæµÄ·½·¨
Õý³£µÄд·¨ÊÇ
if(isLoaded) req({"init": 0});ÁíÍâ×¢ÒâÒ»µã£ºisLoaded=true ÒªÔÚcompleteÀï¼Ó£¬Èç¹ûÖ»ÔÚsuccessÀï¼ÓµÄ»°£¬ ÇëÇóʧ°ÜÁ˾Ͳ»»áÂÖѯÔÙÇëÇóÁË¡£complete²»¹Üsuccess»òerror¶¼»áÖ´ÐÐ
´úÂë¾Íµ½ÕâÀïÁË£¬thank you for attention~
ÒÔÉϾÍÊÇajaxʵʱˢд¦ÀíµÄʵÏÖ·½·¨µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢phpÖÐÎÄÍøÆäËüÏà¹ØÎÄÕ£¡
¼òÒ×·þÎñÆ÷£ºÔÚÃüÁîÖ´ÐеÄËùÔÚ·¾¶Æô¶¯Ò»¸öhttp·þÎñÆ÷£¬È»ºóÄã¿ÉÒÔͨ¹ýä¯ÀÀÆ÷·ÃÎʸ÷¾¶ÏµÄËùÓÐÎļþ¡£
ÔÚ¾ÖÓòÍøÄÚ´«Îļþ£¬»òÕß×Ô¼º²âÊÔʹÓö¼ÊǷdz£·½±ãµÄ¡£
1 »ùÓÚpython1.1 »ùÓÚPython2
python -m SimpleHTTPServer port
> python -m SimpleHTTPServer 8099 Serving HTTP on 0.0.0.0 port 8099 ... 127.0.0.1 - - [24/Oct/2017 11:07:56] "GET / HTTP/1.1" 200 -1.2 »ùÓÚpython3python3 -m http.server port
> python3 -m http.server 8099 Serving HTTP on 0.0.0.0 port 8099 (:8099/) ... 127.0.0.1 - - [24/Oct/2017 11:05:06] "GET / HTTP/1.1" 200 - 127.0.0.1 - - [24/Oct/2017 11:05:06] code 404, message File not found 127.0.0.1 - - [24/Oct/2017 11:05:06] "GET /favicon.ico HTTP/1.1" 404 -2 »ùÓÚnodejsÊ×ÏÈÄãÒª°²×°nodejs
// °²×° npm -g install puer // ʹÓà puer - Ìṩһ¸öµ±Ç°»òÖ¸¶¨Â·¾¶µÄ¾²Ì¬·þÎñÆ÷ - ËùÓÐä¯ÀÀÆ÷µÄʵʱˢУº±à¼cssʵʱ¸üÐÂ(update)Ò³ÃæÑùʽ£¬ÆäËüÎļþÔòÖØÔØ(reload)Ò³Ãæ - Ìṩ¼òµ¥ÊìϤµÄmockÇëÇóµÄÅäÖù¦ÄÜ£¬²¢ÇÒÅäÖÃÒ²ÊÇ×Ô¶¯¸üС£ - ¿ÉÓÃ×÷´úÀí·þÎñÆ÷£¬µ÷ÊÔ¿ª·¢¼ÈÓзþÎñÆ÷µÄÒ³Ã棬¿ÉÓëmock¹¦ÄÜÅäºÏʹÓà - ¼¯³ÉÁËweinre£¬²¢Ìṩ¶þάÂëµØÖ·£¬·½±ãÒƶ¯¶ËµÄµ÷ÊÔ - ¿ÉÒÔ×÷ΪconnectÖмä¼þʹÓÃ(Ç°ÌáÊǺó¶ËΪnodejs£¬·ñÔòÇëʹÓôúÀíģʽ)
ÒÔÉϾÍÊǴ¼òÒ×¾²Ì¬Îļþhttp·þÎñÆ÷µÄ·½·¨µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢phpÖÐÎÄÍøÆäËüÏà¹ØÎÄÕ£¡
ÔÚ³õʼ»¯Ê±£¬Ö§³ÖËĸö²ÎÊý£¬Óÿոñ·Ö¸î£º
ÎÒÃÇÀ´¿´ÏÂÆäʵÀýʹÓãº
let cl = $.Callbacks('once memory unique stopOnFalse'); fn1 = function (data) { console.log(data); }; fn2 = function (data) { console.log('fn2 say:', data); return false; }; cl.add(fn1); cl.fire('Nicholas'); // Nicholas // ÓÉÓÚÎÒÃÇʹÓÃmemory²ÎÊý£¬±£´æÁËÖ´Ðл·¾³£¬ÔÚÌí¼Óеĺ¯Êýʱ×Ô¶¯Ö´ÐÐÒ»´Î cl.add(fn2); // fn2 say: Nicholas // ÓÉÓÚÎÒÃÇʹÓÃonce²ÎÊý£¬ËùÒÔÖ»ÄÜÖ´ÐУ¨fire£©Ò»´Î£¬´Ë´¦ÎÞÈκÎÊä³ö cl.fire('Lee'); // ºóÃæÎÒÃǼÙÉèÕâÀïûÓд«Èëonce²ÎÊý£¬Ã¿´Îfire¶¼¿ÉÒÔÖ´ÐÐ cl.fire('Lee'); // Lee fn2 say: Lee // Çå¿ÕÁбí cl.empty(); cl.add(fn2, fn1); // ÓÉÓÚÎÒÃÇÉèÖÃÁËstopOnFalse£¬¶øfn2·µ»ØÁËfalse£¬ÔòºóÌí¼ÓµÄfn1²»»áÖ´ÐÐ cl.fire('Nicholas'); // fn2 say: Nicholas // ÉÏËøcl£¬½ûÓÃÆä²Ù×÷£¬Çå³ýÊý¾Ý£¬µ«ÊÇÎÒÃÇÌí¼ÓÁËmemory²ÎÊý£¬ËüÒÀÈ»»á¶ÔºóÐøÌí¼ÓµÄÖ´ÐÐÒ»´Î cl.lock(); // ÎÞÏìÓ¦ cl.fire(); cl.add(fn2); // fn2 say: Nicholas // ½ûÓÃcl£¬½ûÖ¹Ò»ÇвÙ×÷£¬Çå³ýÊý¾Ý cl.disable();³ýÁËÉÏÃæËù˵µÄÖ÷Òª¹¦ÄÜ£¬»¹Ìṩhas/locked/disabled/fireWith/firedµÈ¸¨Öúº¯Êý¡£
¡¡