English 中文(简体)
窗户:地点:重载()
原标题:window.location.reload(); reloads page uncontrollably?
  • 时间:2011-08-31 09:21:59
  •  标签:
  • jquery
  • json

谁能解释为什么? 我在此守则:

<script>
$(document).ready(function()
{
    refresh();
});

function refresh()
{      
    $.getJSON( getMessageDetails.php , function (json) {
        //alert(json.length);
        //$("#subject1").html(json[0].subject);
        //$("#unique_code1").html(json[0].unique_code);  
        $("#msg_id").html(json[0].id);
        $("#subject").html(json[0].subject);
        $("#unique_code").html(json[0].unique_code);  
        if (json.length > 0 )
        {
            //alert(json.length);
            window.location.reload(); 
        }     
        else
        {
            //do something
        } 
    }); 
    window.setTimeout(refresh,30000);
}
</script>

What I am trying to do is if a new message came in json wont be empty so reload the page and if there are no new messages, keep on checking

What happens now is a new message comes in and the screen just starts flashing like crazy! It must do the reload once and then every 30 seconds check again. Any help please? Thank you.

最佳回答

我认为,你正在寻找<代码>Interval,因为它与你一样,希望每隔30秒将你的职责称为你,而不是重载整页(window.place.reloaddoes)。

例:

function sayHello(){
alert( hello once again );
}
setInterval(sayHello,30000);

因此,你必须做的是,把你的宇宙航空研究开发机构的呼吁总结为一项功能,然后适用一个<条码>,即“Interval,在接下去时加以界定。

问题回答

我认为,你应当:

$(document).ready(function()
{
    var int = setInterval(refresh,30000);
});

function refresh()
{      
$.getJSON( getMessageDetails.php , function (json) {
 //alert(json.length);
 //$("#subject1").html(json[0].subject);
 //$("#unique_code1").html(json[0].unique_code);  
 $("#msg_id").html(json[0].id);
 $("#subject").html(json[0].subject);
 $("#unique_code").html(json[0].unique_code);  
 if (json.length > 0 )
 {
    //alert(json.length);
    window.location.reload(); 
 }     
 else
 {
    //do something
 } 
 }); 

 }




相关问题
JQuery/MVC Search Issue

I have inherited a piece of work where the entry screen shows a summary of 20 calculated variables. E.g. Var A (250), Var B (79). Clicking on any of these links takes the user to a view with a ...

jQuery quicksearch plug-in tinkering with JSON

I ve implemented the quicksearch plugin by Rik Lomas and I love it for an application in a custom CMS I m building. I was wondering though, since I m going to have a bizillion items in the table if ...

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

PHP json_decode question

i m trying to use json_decode to combine a few json objects and then re-encode it. my json looks like: { "core": { "segment": [ { "id": 7, "...

Converting JSON data to Java object

I want to be able to access properties from a JSON string within my Java action method. The string is available by simply saying myJsonString = object.getJson(). Below is an example of what the string ...

热门标签