简体   繁体   English

如何让JS脚本为每个查看它的人更新页面?

[英]How can I have a JS script update a page for everyone viewing it?

I'm creating a web application that allows users to make changes through Javascript. 我正在创建一个允许用户通过Javascript进行更改的Web应用程序。 There is not yet any AJAX involved, so those changes to the DOM are being made purely in the user's local browser. 尚未涉及任何AJAX,因此对DOM的更改纯粹是在用户的本地浏览器中进行的。

But how can I make those DOM changes occur in the browser of anyone else who is viewing that page at the time? 但是,如何在当时正在查看该页面的任何其他人的浏览器中进行这些DOM更改? I assume AJAX would be involved here. 我假设AJAX将参与其中。 Perhaps the page could just send the entire, JS-modified source code back to the server and then the other people viewing would receive very frequent AJAX updates? 也许该页面可以将整个JS修改后的源代码发送回服务器,然后查看的其他人会收到非常频繁的AJAX更新?

Screen sharing would obviously be an easy work-around, but I'm interested to know if there's a better way, such as described above. 屏幕共享显然是一个简单的解决方法,但我很想知道是否有更好的方法,如上所述。

Thanks! 谢谢!

You are talking about comet , for an easy implementation i'd suggest: 你在谈论彗星 ,为了一个简单的实现,我建议:

and also check these: 并检查这些:

and new html5 way of it 和新的html5方式

Hope these help. 希望这些帮助。

Max, 马克斯,

Ajax will have to be involved. Ajax将不得不参与其中。 If i may, I'd like to suggest jQuery as a starting point for this (i know you didn't tag as such, but i feel it'd be appropriate, even if only to prototype with). 如果可以的话,我想建议jQuery作为这个的起点(我知道你没有这样标记,但我觉得它是合适的,即使只是为了原型)。 the basic semantics would involve running the ajax request in combination with a setInterval() timer to fire off the ajax request. 基本语义将涉及与setInterval()计时器一起运行ajax请求以触发ajax请求。 this could be done in jQuery along the lines of: 这可以在jQuery中完成:

$(document).ready(function() {
    // run the initial request
    GetFreshInfo();
    // set the query to run every 15 seconds
    setInterval(GetFreshInfo, 1500);
});

function GetFreshInfo(){
    // do the ajax get call here (could be a .net or php page etc)
    $.get('mypageinfostuff.php', null, function(data){$('#myDivToUpdate').html(data);});
}

that's the basic premise... ie the webpage is loaded via GetFreshInfo() initially straight away, then it's requeried every 15 seconds. 这是基本的前提......即网页最初是通过GetFreshInfo()加载的,然后每15秒重新获得一次。 you can add logoc to only refresh the div if there is new data there, rather than always updating the page. 如果有新数据,您可以添加logoc以仅刷新div,而不是始终更新页面。 as it's ajax, the page won't freeze and the process will be almost invisible to the user (unless you want to flag the changes in any way) 因为它是ajax,页面不会冻结,并且用户几乎看不到该过程(除非您想以任何方式标记更改)

Hope this helps 希望这可以帮助

jim 吉姆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM