JavaScript Alternatives Method SetInterval and ClearInterval in Android

JavaScript Alternatives Method SetInterval and ClearInterval in Android

 
Javascript Setinterval

Hello Friends as We know Switching from Web Developers to Android we Stuck in Some Situation of Methods and How we use Such Methods.

Here i Give a simple Method of JavaScript Which is.

SetInterval = for Creating Task Multiple Times Until we Not Stop it.
Clearinterval = For Stoping SetInterval Task.

Lets Create a Demo page and see How it Works?


<!DOCTYPE html>
<html>
<head>
 <title>Set Interval and Clear interval in JavaScript</title>
</head>
<body>
<p>Example of Set interval and Clear interval</p>
<div id="data">
 
</div>
<script type="text/javascript">
 var myHtmlDiv=document.getElementById("data");
 var loop=0;
 var s=setInterval(function(){
  myHtmlDiv.innerHTML+='A Text After 1 seconds <br>';
 
  if(loop==10){
   //stop my setinterval function after 10 times if you want infinte times executing then remove the if condition means no clearinterval method
   clearInterval(s);
  }
  loop++;
 },1000);
</script>
</body>
</html>


Result :

Javascript Setinterval



Now Lets How We Use this Methods in Android?

So We Can use This Method Simply in Android by Using this Methods

 
      final Timer timer= new Timer();
        timer.scheduleAtFixedRate(new TimerTask(){
            @Override
            public void run(){
                testi++;
                if(testi==5){
                    Log.i("Complete","TaskComplete");
                    timer.cancel();
                }
                Log.i("tag", "A Kiss every 5 seconds");
            }
        },0,5000);
Previous Post
Next Post

post written by:

0 comments: