简体   繁体   中英

How to stop page from auto refreshing if the visitor is adding a reply / keystroke with my current code?

I am using the following code to auto refresh my website page after a specific time interval. However, users sometimes are in the process of answering a forum discussion or have already entered some answer on the page but because of the refresh that gets lost. How to I avoid this from happening.

I want the page to refresh after the time limit set by me if the visitor has not or is not entering any reply.

<script>
<!--

var limit="2:14"

if (document.images){
    var parselimit=limit.split(":")
    parselimit=parselimit[0]*60+parselimit[1]*1
}
function beginrefresh()  
{
    if (!document.images)
        return
    if (parselimit==1)
       window.location.reload()
    else{
       parselimit-=1
       curmin=Math.floor(parselimit/60)
       cursec=parselimit%60
       if (curmin!=0)
          curtime=curmin+" minutes and "+cursec+" seconds left until page refresh!"
       else
          curtime=cursec+" seconds left until page refresh!"
     <!-- window.status=curtime -->
     window.status = " Welcome To MySite"
     setTimeout("beginrefresh()",1000)
    }
}

window.onload=beginrefresh
//-->
</script>

In your Refresh function beginrefresh() you should check weather user had enter some text in your answer text field and if yes than return from your refresh function like you have done for

if (!document.images)
    return

By this you prevent from auto refresh.
For Example :

if(document.getElementById("Your_Answer_Textbox_Id").value.length == 0)  
   return;

So, first: consider having your timer kick off AJAX queries to update specific portions of the page, rather than reloading the whole thing. That way, you can avoid reloading the text entry area and skip this problem entirely.

In the short term, however, compare the contents of the text input to their default. You haven't included your HTML, so I don't know the proper names, but something like:

if (window.document.getElementById("test").value != "")
   window.onload=beginrefresh;

would suffice. I find this preferable to checking focus, because the textarea may lose focus (while they copy something for a reply, for instance) but still have contents the user does not want to lose.

First use a var to setTimeOut function

        var timer = setTimeout(beginrefresh, 200);

Now use event handlers

    $( "#reply_textArea" ).keydown(function() {
        clearTimeout(timer);
    });

    $( "#reply_textArea" ).keyup(function() {
        timer = setTimeout(beginrefresh, 200);
    });

You can use boolean flags, and check text box values with this to apply logic according to your requirements. As for now to stop beginRefresh above code will do

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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