简体   繁体   English

如何在不手动调用 javascript 中的 function 的情况下使用 ajax 自动刷新页面的一部分?

[英]How do I use ajax to auto refresh a section of page without manually invoking a function within javascript?

var xmlhttp;

//Set up ajax first so he knows which guy to play with
function loadXMLDoc(url,cfunc)
{
    //Code to catch modern browsers
    if (window.XMLHttpRequest)
    {
        xmlhttp=new XMLHttpRequest();
    }

    //Code to catch crap browsers
    else
    {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    //Set up
    xmlhttp.onreadystatechange=cfunc;
    xmlhttp.open("GET",url,true);
    xmlhttp.send();
}

//Set a function to deploy when something calls myFunction()
function myFunction()
{
    loadXMLDoc("../../../support/ajaxTest.txt",function()
    {
        //Fires off when button pressed
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("statusRefresh").innerHTML=xmlhttp.responseText;
            setInterval( "alert('Hello I did something but i needed to be invoked by a button first')", 5000 );  
        }

    });
}

I want to call restful java service to refresh a 'status'.我想调用宁静的 java 服务来刷新“状态”。 I need ajax to auto refresh the this status once the page has been hit.一旦页面被点击,我需要 ajax 自动刷新此状态。 The Refresh method isnt instantaneous, for it has to talk with other machines. Refresh 方法不是即时的,因为它必须与其他机器对话。

function autoRefresh()
{
    var url    = "../../../support/ajaxTest.txt";
    var target = document.getElementById("statusRefresh");

    var doRefresh = function() 
    {
        loadXMLDoc(url, function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                target.innerHTML=xmlhttp.responseText;
            }
        }
    });

    setInterval( doRefresh, 5000 );  
} 

and

document.onload = autoRefresh;

more information is needed such as what is your goal, what do you currently have......what are you trying to do......If its a script thats triggered by something from example a user viewing your page or click a button then use that button to triiger the function to auto refresh需要更多信息,例如你的目标是什么,你目前有什么......你想做什么......如果它的脚本是由例如用户查看你的页面或单击一个按钮,然后使用该按钮触发 function 以自动刷新

Another way is to use a crob job另一种方法是使用 crob 工作

暂无
暂无

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

相关问题 自动刷新 PHP Function 无需重新加载页面 Javascript / Z3EB7106C3477A60E6BBF5DBFDE1DA59 - Auto Refresh PHP Function without reloading page Javascript / Ajax 如何自动刷新页面的一部分 - How to auto refresh a section of a page 如何在不刷新整个页面的情况下使用AJAX刷新div? - How do I refresh a div with AJAX without refreshing the whole page? 如何在不刷新整个页面的情况下使用jQuery自动重新加载页面的一部分? - How do I auto-reload a section of the page with jQuery without refreshing entire page? 如何在 laravel 7 框架中使用 ajax 自动刷新我的页面 - how to use ajax for auto refresh my page in laravel 7 framework PHP \\ MYSQL \\ AJAX(?)-如何在不调用页面刷新的情况下动态更新表单 - PHP\MYSQL\AJAX(?) - Struggling with how to dynamically update form without invoking page refresh 使用Ajax jQuery刷新页面而不使用重新加载功能 - refresh page with ajax jquery without use of reload function 如何在不使用Javascript调用参数的情况下将参数传递给引用函数 - How do I pass an argument to referenced function without invoking it using Javascript 如何使用ajax / json刷新页面部分? - How to refresh a page section using ajax/json? 如何刷新页面数据以显示通过ajax调用更改的动态表数据,而无需重新加载页面? - How do I refresh page data to display dynamic table data that was altered with an ajax call without reloading the page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM