showing a loading image when ajax start running in background

浪子不回头ぞ 提交于 2019-12-11 19:08:34

问题


I have the following script which starts running when i click on record button . I want to show a image when i click on record button which is same as the one displayed usually when we upload images on... this is the script:

<script>
function st()
{
    if (window.XMLHttpRequest)
    {
        xmlhttp=new XMLHttpRequest();
    }
    else
    {
        xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        alert('RECORDING Start');
        }

    }
    xmlhttp.open('GET','http://localhost/IPCAM/start.php;)

    xmlhttp.send();

setTimeout('st()',5000);
}
</script> 

And this the button when i click on this button the ajax starts background.

<button OnClick="st()">Record</button>

I have no Idea to do this please can someone help me .


回答1:


To begin with I find your question very unclear, but let me try:

Make sure that you have a css class that defines the image:

.recordingBackground
{
    background-image: url("record.png");
}

(Or just use javascript)

Create the button:

<input type="button" id="recordButton" value="Record" />

Make jQuery listen to the button and you can add the class to the element:

$('#recordButton').live("click", function()
{
    $('#background').addClass('recordingBackground');
    // Or use a similar method (for example: pure javascript).
    // #background is just an example div.
});



回答2:


Add a div with id loadingDiv and change your code to this

<script>
function st()
{
                        if (window.XMLHttpRequest)
                        {
                            xmlhttp=new XMLHttpRequest();
                        }
                        else
                        {
                            xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
                        }
                        xmlhttp.onreadystatechange=function()
                        {
                            if (xmlhttp.readyState==1)
                            {
                            document.getElementById("loadingDiv").innerHTML = "<img src='loading.gif'/>";
                            }
                            if (xmlhttp.readyState==4 && xmlhttp.status==200)
                            {
                            alert('RECORDING Start');
                            }

                        }
                        xmlhttp.open('GET','http://localhost/IPCAM/start.php;

                        xmlhttp.send();

setTimeout('st()',5000);
}
</script> 


来源:https://stackoverflow.com/questions/15145751/showing-a-loading-image-when-ajax-start-running-in-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!