setInterval() not working after window.open() in Android - javascript

Just a little problem: I'm making a Hybrid app developed in angular with Cordova but when I use a setInterval() function after a $window.open() the setInterval stops working, but in Chrome, it's working (but it's normal, the new window is opened in a new tab).
This is the an extract of my code :
$http.post(url)
.success(function(result, status) {
var windowWithings = $window.open(result.res);
var intervalVerif = $window.setInterval(function (){
$http.get(url)
.success(function(result) {
windowWithings.close();
$window.cancelInterval(intervalVerif);
})
.error(function(error) {
console.log(error);
});
}, 2000);
})
.error(function(error, status) {
console.log(status);
});
Thanks in advance!
EDIT:
What I searching to do with the setInterval is to check each 2000 ms if the server got the information from the new window I have opened. If it's ok for the server I close the window to come back to the previous.
When I say it's not working I want to say that the new window doesn't close but if I change the code with an alert I can see sometimes an alert before the new window had the time to open but nothing after
For Chrome, I just say that if I open the Angular app in Chrome on my laptop it's working, but if I launch my Android app created with Cordova it doesn't work.

please use $timeout and add '$timeout' in injection parameters
$timeout(function (){
$http.get(url)
.success(function(result) {
windowWithings.close();
$window.cancelInterval(intervalVerif);
})
.error(function(error) {
console.log(error);
});, 2000);

Related

Notify users when the browser is minimized

I am using AngularJS & SPring. I need to find a way to notify the users even when the user has minimized the application browser window OR IS there any other better option to notify the users based on a batch job.
Maybe you are looking for something like Browser Desktop Notification.
I just found this LINK and maybe this could help you.
angular.module('app', ['notifyme']). controller('appcontroller', function($scope, NotifyMe) {
$scope.nofity = function(){
var title = "I'm from Angular ?";
var body = 'This is a simple demo for the notification API Angular Service';
NotifyMe.launch(title, {
body: body,
onclick:function(){
console.log("On Click Triggered");
},
onerror:function(){
console.log("On Error Triggered");
},
onclose:function(){
console.log("On Close Triggered");
}
});
};
});
Working Fiddle from the link:

ngCordova capture not working on device

I'm am attempting to record audio using Ionic and ngCordova.
Here is my code:
$scope.captureAudio = function() {
var options = { limit: 3, duration: 10 };
$cordovaCapture.captureAudio(options).then(function(audioData) {
// Success! Audio data is here
alert(audioData);
console.log(audioData);
}, function(err) {
// An error occurred. Show a message to the user
alert(err);
});
};
When I run this in the emulator it works fine and brings up the voice recorder and when the recording is done it logs out the audioData.
When I upload it to ionic view and run it I get nothing. Not even an error.
Has anyone seen anything like this before ?
Thanks
Kevin
$cordovaCapture plugin is not yet supported by ionic view.
Please see the list of supported plugins in this link.
You can run the code in device directly using run command.
ionic run android

Can I use gamepads with Node-Webkit (NW.js)?

I am building a NW.js (Node-Webkit) dashboard app which I want to be able to control with a game controller (for example: XBox 360 controller or Logitech controller).
I'm calling the following onready but when I debug its not recognizing any gamepads.
angular.element(document).ready(function() {
if(canGame()) {
var prompt = "To begin using your gamepad, connect it and press any button!";
$("#gamepadPrompt").text(prompt);
$(window).on("gamepadconnected", function() {
$("#gamepadPrompt").html("Gamepad connected!");
console.log("connection event");
});
$(window).on("gamepaddisconnected", function() {
console.log("disconnection event");
$("#gamepadPrompt").text(prompt);
});
}
});
function canGame() {
return "getGamepads" in navigator;
}
When I debug the code, it doesn't appear to be detecting any gamepads. I also try:
navigator.webkitGetGamepads()
but it doesn't show any gamepads being detected either.
Has anyone successfully used gamepads with a NW.js app?
I would greatly appreciate some help getting this to work.
I found the answer to my issue. Gamepads are indeed compatible with NW.js. I however, was using an old version (~v0.08).
The code I posted earlier when running with NW.js v0.12.2.

Phantom wont initialize/loaded angularjs controller methods

i need help creating a pdf file off a html page powered by an angularjs controller. the pdf will be created by using phantom module. im able to display and bind data correctly as soon as i navigate to the specific url, but when i call phantom to render the page it doesnt load/initiliaze/call the controller methods i have tried different methods such as doing ng-init from the div or calling a function from the controller once is done loading or even using
$scope.$on('$viewContentLoaded', function(){
//code here...
})
phantom code:
function DoThePDF(){
phantom.create(function(ph){
ph.createPage(function(page) {
page.set("paperSize", { format: "A4", orientation: 'portrait', margin: '1cm' });
page.open(address, function(status) {
if(status !== 'success'){
console.log('unable to open webpage');
}
else{
setTimeout(function() {
page.render("reports.pdf");
console.log("page rendered");
ph.exit();
}, 5000);
}
})
})
});
}
when calling page.open(address, function(status) {...} i can see that the page is been opened but the angular events/methods dont get initialized/called so when the page gets rendered is an empty html template no data gets binded. i have added delays but still it doesnt help.
PhantomJS 1.9.x doesn't support websocket, therefor i have to use PhantomJS 2. there is not an official release yet since is being developed but there a beta version
Here build as specified. Good luck

Facebook Javascript SDK login problems in Chrome/Safari (webkit) - Unsafe Javascript attempt

Similar questions have been asked, but the solutions to those problems don't seem to help me.
I've got a local development site set up running on a virtual machine: http://lamp.site
When the index.php loads, I've got this javascript running (with the correct appId):
/* facebook auth */
window.fbAsyncInit = function() {
FB.init({
appId: '01234567890',
channelUrl: 'http://lamp.site/channel.html'
});
};
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
Then when a button is clicked, this javascript function is triggered:
#HTML
<li><a class="btn" href="#" ng-click="connectToFacebook()">Connect with Facebook</a></li>
#Snippet in AngularJS Controller
$scope.connectToFacebook = function() {
facebookConnect.authenticate(
function(reason) { // fail
console.log(reason);
},
function(user) { // success
console.log('logged in',user);
//Session.login();
});
return false;
}
#Factory in AngularJS module
.factory('facebookConnect', function() {
return {
authenticate: function(fail, success) {
FB.login(function(response) {
console.log(response);
if (response.authResponse) {
FB.api('/me', success);
} else {
fail('User cancelled login or did not fully authorize.');
}
});
}
}
If I press the button in Firefox, it works! Lovely, I get all the right things back from Facebook.
If I use Chrome or Safari on my PC, or Safari Mobile on my iPhone, I get the facebook login window popup with the message "An error occurred. Please try again later.", and the following console data:
Unsafe JavaScript attempt to access frame with URL
https://www.facebook.com/dialog/permissions.request from frame with URL
http://lamp.site/index.php?page=login. Domains, protocols and ports must match.
Object
authResponse: null
status: "not_authorized"
__proto__: Object
User cancelled login or did not fully authorize.
I've got my local testing URL added into the facebook app in the developer section, that works because it logs in with Firefox. This is my channels file, stored at http://lamp.site/channel.html
<script src="http://connect.facebook.net/en_US/all.js"></script>
I've tried having the channel file with and without http: in the src= tag.
The facebook app has the following settings:
App domains: lamp.site
Site URL: http://lamp.site
Mobile site URL: http://lamp.site
So it works in Firefox, what the hell am I doing wrong. I can't just enable cross site scripting as users wouldn't do that and this is going into a mobile site.
Anyone else managed to solve this problem recently?
All the other similar questions have gone unanswered... someone must have fixed this!
EDIT: I've made a simplified version, new app ID, on my website.
http://dev.willshawmedia.com/fb/
Here is the screen shot with the app ID from Facebook Dev application panel
You can look at the source code, it's copied straight from here:
https://developers.facebook.com/docs/guides/web/
And the channel file does exist:
http://dev.willshawmedia.com/fb/channel.html
I can't make it any simpler, I still get the "An error occurred. Please try again later." message, but now it just forwards onto Facebook instead of authenticating and closing.
Edit: I was missing the site URLs, I've added those, the simple example is working.
As it's working on my live site, it must be to do with my local domains. But I've got this line in my /etc/hosts/ file:
192.168.0.13 lamp.site
That's the IP address of the VirtualBox Ubuntu server running on my laptop. I can browse that site fine.
Sandbox Mode must be disabled for it to work in Chrome. That's it, that's all it took.
I enabled that mode as I thought that would stop it showing up in searches, which I don't want. And as it worked in Firefox I couldn't see that being a problem. But there you go, I switched off sandbox mode and it started working immediately.
I don't under stand that why that is stopping Chrome though.
Your factory has some strange JavaScript: return new function() ... outside of that I don't see an issue. I'm using this same basic technique and I don't have any problems in Chrome.
Be sure your Facebook application is set up to your domain (let's call it "mydomain.com")... THEN, to test locally, you'll need to edit your hosts file to point mydomain.com to 127.0.0.1... so the URL in the browser location matches what Facebook is expecting.
That's probably the only difference between what I'm doing and what you're doing.
... also make sure your App ID is the proper App ID from setting up your Facebook Application. Double check it. BS "test" App ID's will give you the error message as well.
-
-
EDIT : on the new function(){ } front... which I don't think is your problem.... I want to explain a little more:
var x = new function() { this.foo = 'test'; };
is the same as
var x = { foo: 'test' };
Because the new keyword is used to call a function as an object constructor, and you're using new on an anonymous function that you can't use again, so there is no reason to declare it.
So your snippet could (and probably should) be:
.factory('facebookConnect', function() {
return {
authenticate: function(fail, success) {
FB.login(function(response) {
console.log(response);
if (response.authResponse) {
FB.api('/me', success);
} else {
fail('User cancelled login or did not fully authorize.');
}
});
}
}
});

Categories