comaster mooko6l comaster mooko6l - 1 month ago 10
AngularJS Question

How to loop this function in every 30 second?

Hello building ionic app that get a connecting wifi info and send it to firebase

this function work normally
but I want 1 time button click to loop this function to send value in every 30 second and if button click again will stop loop.

can someone teach me how to do please?

button code

<button
class="button icon-left ion-radio-waves button-positive inactive b_resize"
ng-click='getWifiInfo();'>Send Wifi info
</button>


function code

$scope.getWifiInfo = function() { //get wifiinfo
function success(results) {

$scope.wifiinfo=results;
$scope.$apply();

$scope.currentDate = $filter('date')(new Date(),'EEEE,d MMMM, y');
$scope.currentTime = $filter('date')(new Date(),'h:mm:ss a');


fireBaseData.refUser().child($scope.user_info.uid).child("wifiinfo-record").child($scope.currentDate).child($scope.currentTime).push({



//wifi data send to wifiinfo-record
SSID: $scope.wifiinfo.SSID,
MacAddress:$scope.wifiinfo.BSSID,
SignalStrength: $scope.wifiinfo.Rssi,
type:"record",

});


};
function err(e) {
console.log(JSON.stringify(e));
};
WifiInfo.getWifiInfo(success,err);
}

Answer

You're looking for setInterval

function foo(){
    console.log('function is being called')
}

var interval = setInterval(foo, 30000);

and to stop it you'll do:

clearInterval(interval);