Saturday 28 January 2017

How to make counter not reset on page refresh javascript


How to make counter not reset on page refresh javascript

<form name="counter">
    <input type="text" size="8" name="chandresh" id="counter">
</form>

<script type="text/javascript">
function getCookie(cname) {
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.split(';');
    for(var i = 0; i <ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

var cnt = 60;
function counter(){
    if(getCookie("cnt") > 0){
cnt = getCookie("cnt");
}
cnt -= 1;
document.cookie = "cnt="+ cnt;
jQuery("#counter").val(getCookie("cnt"));

if(cnt>0){
setTimeout(counter,1000);
}

}

counter();
</script>

4 comments:

Unknown said...

Hi sir here we get only seconds if we need 5 minutes and 40 seconds of time duration by that time how we get this formate please explain me

Unknown said...
This comment has been removed by the author.
PHP Coder said...
This comment has been removed by the author.
Unknown said...

Thank you for the information this code almost useful to me i added some extra script to your code then it was working properly in the formate of minutes.