I want to create a timer that will reload extensions every 10 minutes.
This works, except the window doesn't open. I tried it with www.google.com as well
any thoughts?
var count = 0
var jsUpdatePageTimer = setInterval(updatePage, 5000)
function updatePage(){
console.log(count)
if (count == 3) {
console.log('count is 3')
clearInterval(jsUpdatePageTimer)
} else {
console.log('updating count')
window.open('http://reload.extensions')
count +=1
}
}
popup blockers like to block calls to window.open — you may remember the terrible days when javascript could open popups willy-nilly
nowadays, for window.open to work, you have to use it within a user-initiated call stack
which means, you can only use window.open as the result of a user-triggered event, like a mouse click
button.onclick = () => {
window.open(/*...*/)
}
so you need to ask yourself: what is the relevant user action that should trigger this popup? you need to have the user's consent
Related
Whenever I need to make an ajax request to the server without refreshing the page, Genexus should emit some event that I could use in my UC.
What exactly would be this event, and if there is none, how could I know every events in JS to use in an Genexus User Control?
I.e.:
If I click in an UserAction that may search for values in another table and then retrieve then for me, how can I capture this?
Another Scenario:
I'm making a request to the server and retrieving information of it. I need to get the
$(gx-warning-message).text();
but if my request is still loading, when I execute my function, it will return nothing, because the event is still loading up informations.
To avoid this, I'm making a looping, but it's not an elegant way to solve the issue.
$(".Button").click(function() {
var timesRun = 0;
var interval = setInterval(function(){
timesRun += 1;
if(timesRun === 60){
clearInterval(interval);
}
if ($(".gx-warning-message").text().length > 0) {
toastrgx();
$(".gx-warning-message").text('');
}
}, 200);
});
So, how can I make it better?
Thanks
If you want to be notified every time a GeneXus user event is fired, you can subscribe to gx.onafterevent event. For example:
gx.fx.obs.addObserver('gx.onafterevent', scope, function () {
// Here goes the code you want to execute
// every time a GeneXus event is fired
});
scope is the object you want to set as the this of the third parameter function.
I have a timeout function
var idleTimeOut = 60; // 1 minute
ResetIdleSecondsEvent = function () {
// Reset counter for all listed event
$(document).on("click mousemove keypress touchstart touchend touchcancel touchmove", function () {
_idleSecondsCounter = 0;
})
}
function VerifyTimeOutInterval() {
// Run every seconds to call function and check if current counter is more than
// the preset time
window.setInterval(CheckIdleTime, 1000);
}
function CheckIdleTime() {
// Check timer only predefined timeout value is not 0, null or empty
if (idleTimeOut != "") {
// Increase counter for seconds
_idleSecondsCounter++;
// Check if current counter is more than the preset timeout period
if (_idleSecondsCounter >= idleTimeOut) {
// Notify user that he/she has been automatically logged out
navigator.notification.alert("You have been automatically logged out due " +
"to inactivity. Please login again.", function () { }, "Logged Out", 'OK');
// Navigate to login page
self.navigateLogout();
}
}
}
This works great in my application when user do nothing for 1 minutes, timeout message will appear and take user to login screen.
But when user open an external link using Inappbrowser and has activity, the idle seconds did not get reset, the timeout message appear and take user back to login. So how do I add the same code into Inapp browser?
As far as I know, the Inappbrowser only has the event loadstart, loadstop, loaderror and exit. I tried the following code to add event into Inappbrowser but no luck.
var inappBrowserObj = window.open(url, '_blank', 'location=yes');
inappBrowserObj.addEventListener('loadstop', ResetIdleSecondsEvent());
inappBrowserObj.addEventListener('loadstop', VerifyTimeOutInterval());
Please tell me what is the correct way to handle timeout function inside Inappbrowser.
Thanks
Your code seems to be wrong while implementing the event handlers. Your callbacks are incorrectly passed and also you don't need to register handlers for same event twice (though you can but not required). Your code should be:
inappBrowserObj.addEventListener('loadstop', ResetIdleSecondsEvent);
Observe how I have passed the callback, your code will execute that function instead of passing it as a callback.
Make your other function calls within the ResetIdleSecondsEvent function.
I hope this helps.
I have an interactive program where the user calls one after the other a series of translations of a definite source word into languages that are handled in Google Translate.
The user need not close the child windows himself. That does the program as soon as the user notes his conclusion depending on the kind of translation Google Translate produces.
I have already tried for weeks to solve the problem that the program reports that a certian window can not be closed after the program should have executed page.close().
I am now near to the solution, I think, that the browser needs some time for closing and only after that can detect that the window is closed.
Essentially the code which still does not conform to my wishes reads
function loadpage() {
page=window.open("url-code for calling specific translation by GT", "GT", "scrollbars=1, resizable=1, height=400, top=200, left=300, width=400", true);
}
function crucial() {
page.close();
setTimeOut(function() {
if (page.closed) {
alert("succesful closing");
} else {
alert("The window can't be closed!");
}
}, 5000);
}
Alas ! Neither of the two alerts show up. In some cases, depending on the delay time ?, the window closes.
The advice I got from Bergi to check inside the delayed function by if (!page.closed) did not work.
What works seems to have to fulfill the following conditions:
1. Declare the setTimeOut function.
2. create function preferential in the format function() {alertcheck()}
(the delay time seems not to matter; it works even with 0 anyhow.)
var timeoutID;
function preferential() {
timeoutID = window.setTimeout(function(){alertcheck()}, 0);
}
function alertcheck() {
if (page.closed) {
alert("succesful closing");
} else {
alert("The window could not be closed!");
}
}
Why it is like that interests me a lot. At least I hope, that readers can repeat this experiment with the same result.
the program reports that a certian window can not be closed after the program should have executed page.close()
Closed windows cannot be closed. You should always use
if (!page.closed)
page.close();
In my web page, I have a countdown timer using Javascript's setTimeout().
function Tick() {
if (RemainingSeconds <= 0) {
alert("Time is up.");
return;
}
RemainingSeconds -= 1;
ElapsedSeconds += 1;
UpdateTimerDisplay();
window.setTimeout("Tick()", 1000);
}
I also have a function triggered on onbeforeunload to "prevent" the user from leaving the page.
window.onbeforeunload = function () {
if (!isIEAjaxRequest) {
return "You should use the logout button to leave this page!";
}
else {
isIEAjaxRequest = false;
}
};
The problem is that when the "Are you sure you want to leave this page?" window prompts, it pauses the setTimeout() function. Any thoughts on how to prevent this?
You can't. Javascript is strictly single threaded, so any modal popup will suspend everything else.
A possible workaround would maybe be to use var before = new Date() to store the time before your dialog appears and then use that one to calculate the passed time after your dialog disappears to make up for the missed seconds.
No, you can't keep updating the UI in the background while the UI thread is consumed with another task (in this case, presenting that native modal dialog prompt).
See Javascript timeout - specification
I've noticed that when doing
$(".sitelink_external").click(function(e){
e.preventDefault();
window.open(redirectUrl,'_blank')
});
it opens a new tab or window, without calling the pop-up blocker of Chrome, Firefox or IE.
However, I want to create a redirect so that the user will be able to regret, making a timer for the link, and if I do like this:
$(".sitelink_external").click(function(e){
e.preventDefault();
setTimeout("window.open(redirectUrl,'_blank')", timer * 1000);
});
It calls the popup blocker, and doesn't open the popup the way I want, any idea how to resolve this?
EDIT: the answers are great, but don't resolve the problem, as it was working before as well in the same manner.
You can create a really bad performance synchronous timeout function:
function synchronousTimeout (timeout, handler) {
const start = new Date().getTime()
while ((new Date().getTime() - start) < timeout) {}
handler()
}
And then simply use it with your click handler
$(".sitelink_external").click(function(e){
e.preventDefault();
synchronousTimeout(timer * 1000, () => window.open(redirectUrl,'_blank'));
});
EDIT: I just realised that this questions was from YEAAAARS before, wow :D
Change the setTimeout call to do this instead:
setTimeout(function() { window.open(redirectUrl,'_blank') }, timer * 1000);
You can't pass a variable the way you were doing it (and it is generally better to pass a function rather than a string that will be eval'd)