简体   繁体   English

如何每10秒刷新一次Div,而不刷新整个页面?

[英]How to refresh a Div every 10 seconds without refreshing the entire page?

I'm working on a website platform that doesn't allow for any server sided scripting, so jquery and javascript are pretty much all I have to work with. 我正在一个不允许任何服务器端脚本编写的网站平台上工作,因此jquery和javascript是我需要使用的几乎所有东西。 I am trying to create a script to work with the site that will update a div that contains an inbox message count every 10 seconds. 我正在尝试创建一个脚本来与该站点一起使用,该站点将每10秒更新一个包含收件箱邮件计数的div。 I've been successful with making the div refresh every ten seconds, but the trouble lies in the page views count. 我已经成功地使div每十秒钟刷新一次,但是麻烦在于页面浏览量。 My script is refreshing the whole page and counting for a page view, but I only want to refresh just the one div. 我的脚本正在刷新整个页面并计算页面浏览量,但是我只想刷新一个div。 An example of the trouble my script causes is when viewing anything on the site that has a page view counter (forum posts, blog posts, ect...), the page views go crazy because of the script refreshing. 我的脚本引起的麻烦的一个例子是,当在网站上查看具有页面视图计数器的任何内容(论坛帖子,博客帖子等)时,由于脚本刷新,页面视图变得疯狂。 I'm pretty new to Javascript, so I'm not entirely sure there is a way around this. 我对Java语言还很陌生,所以我不完全确定有办法解决此问题。

What I'm working with is below: 我正在使用的是以下内容:

    <div id="msgalert" style="display: none"; "width: 100px !important">
<a href=""> You have $inbox_msg_count new messages.</a>
</div>

$inbox_msg_count is a call that grabs the message count, and provided by the platform the site is on. $ inbox_msg_count是获取消息数的调用,由站点所在的平台提供。 It displays the message count automatically when used. 使用时,它自动显示消息计数。

Then the script that does all the work is this: 然后,完成所有工作的脚本是这样的:

    <script>
 setInterval(function(facepop){
  var x= document.getElementById("SUI-WelcomeLine-InboxNum");
var z = x.innerText;
 if(x.textContent.length > 0)
$("#msgalert").show('slow');
}, 1000);
facepop();
</script>
<script>
setInterval(function() {  
$("#msgalert").load(location.href+" #msgalert>*","");  
}, 1000); // seconds to wait, miliseconds
</script>

I realize I've probably not done the best job of explaining this, but that's because I'm pretty confused in it myself. 我意识到我可能没有最好地解释这一点,但这是因为我自己对此很困惑。 Like I mentioned previously, this code function just how I want it, but I don't want it to refresh the entire page and rack up the page views. 就像我之前提到的那样,此代码可以按我的意愿运行,但我不希望它刷新整个页面并显示页面视图。 Any help is much appreciated. 任何帮助深表感谢。

You might try to look into iframe and use that as a way to update/refresh your content (div). 您可以尝试研究iframe并将其用作更新/刷新内容(div)的一种方式。 First setup an iframe, and give it an id, then with JS grab the object and call refresh on it. 首先设置一个iframe,并为其指定一个ID,然后使用JS抓取该对象并对其调用刷新。

well your prob seems a little diff so i think submitting a from within the div might help you so ... 好吧,您的问题似乎有些不同,所以我认为从div内提交一个可能会帮助您...

$(document).ready(function() 
{
            // bind 'myForm' and provide a simple callback function
               $("#tempForm").ajaxForm({
               url:'../member/uploadTempImage',//serverURL
               type:'post',
               beforeSend:function()
               {
                   alert(" if any operation needed before the ajax call like setting the value or retrieving data from the div ");
               },
               success:function(e){
                alert("this is the response data simply set it inside the div ");
               }
        });
        });

I think this could probably be done without a form, and definitely without iframes ( shudder ).. 我认为这可能无需表格即可完成,而且绝对不需要iframe(不颤抖 )。

Maybe something like this? 也许是这样的吗?

$(document).ready(function()
{
  setInterval(function(facepop)
     {
        var x= document.getElementById("SUI-WelcomeLine-InboxNum");
        var z = x.innerText;

        if(x.textContent.length > 0)
           $("#msgalert").show('slow');

        $.ajax({
            type: "POST",
            url: location.href,
            success: function(msg)
             {
                $("#msgalert").html(msg);
             }
        });

     },1000);

It's not entirely clear exactly what you're trying to do (or it may just be that I'm ultra tired (it is midnight...)), but the $.ajax() call in the above is the main thing I would suggest. 尚不清楚您到底想做什么(或者可能只是我太累了(午夜……)),但是上面的$ .ajax()调用是我的主要任务会建议。

Encapsulating both functions in a single setInterval() makes things easier to read, and will extinguish the 1 second gap between showing the msgalert element, and "re-loading" it. 将这两个函数封装在单个setInterval()中,使事情更容易阅读,并将消除显示msgalert元素与“重新加载”元素之间的1秒间隔。

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

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