Reputation: 6802
I am using ionic framework and cordova-plugin-shake plugin to detect device shaking for one of my Android application, which is working fine. But the problem is after a shake I would like to disable this shaking detection for 30 second, for which I am trying to use $timeout
like this:
$timeout($scope.watchForShake(), 30000);
But somehow for $timeout
, no matter what the delay value is, $scope.watchForShake()
is executed instantly.
I have also tried using setTimeout
but the result is still the same.
Upvotes: 4
Views: 1857
Reputation: 113
$timeout($scope.watchForShake,30000);
Just remove paranthesis after $scope.watchForShake.
Upvotes: 3
Reputation: 49590
$timeout
(and setTimeout
) expect a callback function as its first parameter - that is the function that will execute after a certain time-out.
If you want the function .watchForTimeout
to execute, then pass that function itself as a first parameter:
var callbackFn = $scope.watchForTimeout;
$timeout(callbackFn, 30000);
After 30 seconds, the function callbackFn
will be invoked with no parameters: callbackFn()
.
In your case, you are invoking $scope.watchForTimeout
right away, thus passing the return value of that function as the first parameter to `$timeout. So, what you are currently doing (incorrectly) is:
var returnVal = $scope.watchForTimeout();
$timeout(returnVal, 300000)
Upvotes: 4