Abd Abughazaleh
Abd Abughazaleh

Reputation: 5535

Android & Javascript webview timeout called more time

I built a website using PHP & JavaScript and also I built an android WebView activity each page in website have a activity in android i'm trying to setTimeout in JavaScript 10 sec and this timeout will update when user clicked anywhere on screen , JavaScript code :

var timer = null;

timer = timerAction();

$(window).click(function () {
    clearTimeout(timer);
    timer = timerAction();
});

function timerAction() {
    return setTimeout(function () {
        showToast("Timeout relogin agagin");
        loadNewPage('LoginActivity');
    }, 10000);
} 

and my android code to going another page :

@JavascriptInterface
public void loadeNewPage(String page) {
    if (page.equalsIgnoreCase("MainMenuActivity")) {
        Intent intent = new Intent(context, MainMenuActivity.class);
        finish();

        startActivity(intent);

    }
}

my problem , example : if i clicked 10 times on screen timeout it will called 10 times again .

Upvotes: 2

Views: 895

Answers (1)

Basil jose
Basil jose

Reputation: 780

i know its bit late anyway please go through this , it may help you

 function Timeout(fn, interval) 
    {

        var id = setTimeout(fn, interval);
        this.cleared = false;
        this.clear = function () {
            this.cleared = true;
            clearTimeout(id);
        };
    }

    var t = new Timeout(function () 
    {
       console.log("Cleared function");
       t.clear();
       showToast("Timeout relogin again");
       loadNewPage('LoginActivity');

    }, 5000);



    $(window).click(function () {
        if (t.cleared) 
        {
            t = new Timeout(function () 
                 {
                  console.log("Cleared function");
                  t.clear();
                   showToast("Timeout relogin again");
                   loadNewPage('LoginActivity');

                }, 5000);

        }
        else {
            console.log("one time out funtion already running ");
        }

    });

Upvotes: 2

Related Questions