0

I am quite new to Javascript. I am trying to implement a button that controls whether a page is refreshing or not. However, it doesnt work (clicking on the button triggers some actions like alert etc, but doesnt change the label button and doesnt stop refreshing the page).

Can you tell me where I went wrong, please? Here is my code:

<!DOCTYPE html>
<html>
<head>
    <title>Message log</title>
    <link href="styles.css" rel="stylesheet" type="text/css">
</head>

<body onload="JavaScript:timedRefresh(5000);">

    <button id="bRefresh" onclick="refreshMode()">Stop refreshing</button>

    some content...

</body>
</html>

<script type="text/JavaScript">
    <!--
        var refresh = 1;

        function timedRefresh(timeoutPeriod) {
            if(refresh == 1) {
                setTimeout("location.reload(true);",timeoutPeriod);
            }       
        }

        function refreshMode() {
            if(refresh == 1) {
                var refresh = 2;
                document.getElementById("bRefresh").value="Start refreshing";
            } else {
                var refresh = 1;
                document.getElementById("bRefresh").value="Stop refreshing";
            }       
        };
    //   -->
</script>
1
  • this wont work at all..Because once page starts reloading javascript will stop excecuting Commented Apr 22, 2013 at 9:14

4 Answers 4

1

Your code should look like below,

        var refresh = 1;
        var timedInterval= null;
        function timedRefresh(timeoutPeriod) {
            if(refresh == 1) {
                timedInterval = setInterval(function() { location.reload(true); },timeoutPeriod);
            }       
        }

        function refreshMode() {
            if(refresh === 1) {
                 refresh = 2;
                 clearInterval(timedInterval);
                 document.getElementById("bRefresh").innerHTML="Start refreshing";
            } else {
                 refresh = 1;
                 timedRefresh(5000);
                document.getElementById("bRefresh").innerHTML="Stop refreshing";
            }     
            //document.getElementById("bRefresh").innerHTML="Start refreshing";  
        };

Use setInterval instead of setTimeout for repeated tasks

you declared refresh variable three times so your buttons value doesn't change.

innerHTML instead of value should be used to change the Buttons text.

To stop use clearInterval as above.

Sign up to request clarification or add additional context in comments.

Comments

0

use innerHTML for supporting IE < 9

var button = document.getElementById("bRefresh");
var refresh = 1;

function refreshMode() {
    if (refresh == 1) {
        refresh = 2;
        button.innerHTML = "Start refreshing";
    } else {
        vrefresh = 1;
        button.innerHTML = "Stop refreshing";
    }
};

button.addEventListener("click", refreshMode, false);

Comments

0
<button id="bRefresh">Stop refreshing</button>

var button = document.getElementById("bRefresh");
var refresh = 1;
var timeoutPeriod = 5000;
var id;

function timedRefresh() {
    if (refresh == 1) {
        window.location.reload(true);
    }
}

function refreshMode() {
    if (refresh == 1) {
        clearInterval(id);
        refresh = 2;
        button.textContent = "Start refreshing";
    } else {
        id = setInterval(timedRefresh, timeoutPeriod);
        refresh = 1;
        button.textContent = "Stop refreshing";
    }
};

button.addEventListener("click", refreshMode, false);

id = setInterval(timedRefresh, timeoutPeriod);

on jsfiddle

Comments

0

On the load of the body, prepareRefresh() function check the value refresh (set to 1) and start refresh the page with a timeout. You should launch the refresh, and just before refresh, you had to check the refresh value. Because in the first case, location.reload don't care about modification of refresh. Check the refresh value at the end of the timeout

<script type="text/JavaScript">
    <!--
        var refresh = 1;

        function timedRefresh(timeoutPeriod) {
            if(refresh == 1) {
                setTimeout("refreshPage();",timeoutPeriod);
            }       
        }

        function refreshPage ()
        {
          if(refresh == 1) {
                setTimeout("location.reload(true);", 0);
            }  
        }

        function refreshMode() {
            if(refresh == 1) {
                refresh = 2;
                document.getElementById("bRefresh").innerHTML="Start refreshing";
            } else {
                refresh = 1;
                document.getElementById("bRefresh").innerHTML="Stop refreshing";
            }       
        };
    //   -->
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.