2016-12-20 53 views

回答

0

如果我正确理解你的问题,你可以使用普通的JavaScript来实现这样的事情。下面是做服务器请求每五秒钟一个例子:

function req() { 
 
    fetch('http://reqres.in/api/users', { 
 
    method: 'post', 
 
    body: JSON.stringify({ 
 
     name: 'morpheus', 
 
     job: 'leader' 
 
    })}) 
 
    .then(res => res.json()) 
 
    .then(json => { 
 
     document.getElementById('View').innerHTML = json.id; 
 
    }); 
 
} 
 

 
req(); 
 
setInterval(() => req(), 5000);
<div id="View"></div>

也有库,使这更容易,例如PollJS。你可以在Github上找到更多。如果您可以控制服务器,则可能需要检出Socket.io