Perhaps I am over-thinking this but what I need to do is see what the time difference it is from the start session time (1 minute) subtracting the current time.
<script type="text/javascript">
var mySessionTimer;
#functions {
public int PopupShowDelay
{
get {
DateTime currentSetTimeout = DateTime.Now.AddMinutes(HttpContext.Current.Session.Timeout);
DateTime currentServerTime = DateTime.Now;
TimeSpan duration = (currentServerTime.Subtract(currentSetTimeout));
return 60000 * (int)duration.TotalMinutes;
}
}
}
function callJSSessionTimer() {
var sessionTimeoutWarning = #PopupShowDelay;
var sTimeout = parseInt(sessionTimeoutWarning);
mySessionTimer = setTimeout('SessionEnd()', sTimeout);
}
function SessionEnd() {
clearTimeout(mySessionTimer);
window.location = "/Account/sessionover";
}
#if (userInfo != null)
{
if (userInfo.chosenAMT == "True")
{
#:callJSSessionTimer();
} else
{
#:clearTimeout(mySessionTimer);
}
} else {
#:clearTimeout(mySessionTimer);
}
</script>
So for the value for duration is -00:01:00 which technically is correct since currentSetTimeout is 1 minute and it gets todays date/time which is a minute away since its subtracting it fromcurrentSetTimeout.
So the point of all of this is to keep track of the remaining session time when the user jumps from page to page. Currently when the user goes to another page, it resets the time and its not accurate.
How can I go about doing this the way I need it?
You may use html5 session storage to maintain the value when a user goes to another page during the session:
if (sessionStorage.popupShowDelay) {
sessionStorage.popupShowDelay = Number(sessionStorage.clickcount);
} else {
DateTime currentSetTimeout = DateTime.Now.AddMinutes(HttpContext.Current.Session.Timeout);
DateTime currentServerTime = DateTime.Now;
TimeSpan duration = (currentServerTime.Subtract(currentSetTimeout));
sessionStorage.popupShowDelay = 60000 * (int)duration.TotalMinutes;
}
You can see more in formation here :
https://www.w3schools.com/html/html5_webstorage.asp
Conceptually your problem is the counter would reset as you go from page to page. So you have to keep start time of the session server side. If you are using ASP.NET, you'd use a session variable to do this. Other platforms have similar things. They all work by using cookies. Good luck!
Got it!
#functions {
public int PopupShowDelay
{
get {
if (Session["currentSetTimeout"] != null)
{
DateTime dt1 = DateTime.Parse(Session["currentSetTimeout"].ToString());
DateTime dt2 = DateTime.Parse(DateTime.Now.ToString());
TimeSpan span = dt1 - dt2;
return (int)span.TotalMilliseconds;
}
return 0;
}
}
}
Related
New date does not overwrite executionContext date retrived.
Hi!
I have a javascript that's calculating the end date based off the start date. The function triggers onChange for the start date field. But also before that, sets a default time for both start- and end date. This only happens when it is a create form.
The issue here is that when opening a create form, I retrieve the start and end date from the executionContext and sets the default time for both fields. Afterwards the onChange function runs, where we once again retrieves the start and end date fields from executionContext, calculates the new end date based off the retrieved start date, and makes sure that the previous end date time is set on the new end date. The time that I get is the first one caught by the executionContext and not the default time that I updated it to.
Is there some way for me to get the new value (the default time) and fetch that in the onChange function via the executionContext, without having to create a new separate js? When is the executionContext updated, because the form does get the default end time for a millisecond before getting the onChange value.
if (typeof (FA) == "undefined") { FA = {}; }
FA.Event = {
formContext: null,
OnLoad: function (executionContext) {
this.formContext = executionContext.getFormContext();
// Run on Create form
if (this.formContext.ui.getFormType() === 1) {
if (this.formContext.getAttribute("course_id").getValue() != null) {
FA.Event.SetEndDate(executionContext);
}
if (this.formContext.getAttribute("startdate").getValue() != null) {
FA.Event.SetDefaultStartTime(executionContext);
} else {
alert("startdate was null");
}
if (this.formContext.getAttribute("enddate").getValue() != null) {
FA.Event.SetDefaultEndTime(executionContext);
} else {
alert("enddate was null");
}
}
// Activates onchange events
this.formContext.getAttribute("startdate").addOnChange(FA.Event.SetEndDate);
this.formContext.getAttribute("course_id").addOnChange(FA.Event.SetEndDate);
},
SetDefaultStartTime: function (executionContext) {
var formContext = executionContext.getFormContext();
var startDate = formContext.getAttribute("startdate").getValue();
startDate.setHours(9, 30, 0);
formContext.getAttribute("startdate").setValue(startDate);
},
SetDefaultEndTime: function (executionContext) {
var formContext = executionContext.getFormContext();
var endDate = formContext.getAttribute("enddate").getValue();
endDate.setHours(17, 0, 0);
formContext.getAttribute("enddate").setValue(endDate);
},
SetEndDate: function (executionContext) {
var formContext = executionContext.getFormContext();
var startDate = formContext.getAttribute("startdate").getValue();
var endDate = formContext.getAttribute("enddate").getValue();
if (formContext.getAttribute("course_id").getValue() != null) {
// Get course
var courseId = formContext.getAttribute("course_id").getValue()[0].id;
// Get days
Xrm.WebApi.retrieveRecord("course", courseId, "?$select=days").then(
function success(result) {
console.log("Retrieved values: Days: " + result.days);
// Round days up
var days = Math.ceil(result.days * Math.pow(10, 0)) / Math.pow(10, 0);
console.log("Days rounded up where decimal result: " + days)
var newEndDate = new Date(startDate);
newEndDate.setHours(endDate.getHours(), endDate.getMinutes(), 0);
newEndDate = addDays(newEndDate, farDays);
alert("newenddate: " + newEndDate);
//sets enddate
formContext.getAttribute("enddate").setValue(newEndDate);
},
function (error) {
console.log(error.message);
// handle error conditions
}
);
}
else {
console.log("End date was not calculated.");
}
function addDays(date, days) {
var newDate = new Date(date);
newDate.setDate(date.getDate() + days);
return newDate;
}
}
}
I solved this by using global variables for start and end date, for which I set the values for from the executionContext. If the functions setDefaultEndTime or SetDetfaultStartTime runs, they update the variable for the SetEndDate function to use, otherwise original value is used.
I did not find anything about updating the executionContext or such.
I am trying to store information with the webstorage API. I created a class but the console returns the message "undefined". I was expecting the console to return 20 and 60. Can you help me identify my mistake? Thank you :)
class Timer {
constructor(minutes, secondes){
this.minutes = minutes;
this.secondes = secondes;
this.minutesEltHtml = document.getElementById("minutes");
this.secondesEltHtml = document.getElementById("secondes");
}
setMinutesSecondes(){
sessionStorage.setItem("minutes", this.minutes);
sessionStorage.setItem("secondes", this.secondes);
}
getMinutesSecondes(){
return sessionStorage.getItem("minutes");
return sessionStorage.getItem("secondes");
}
display(){
console.log(timer.getMinutesSecondes());
//console.log(timer.this.minutes);
}
}
let timer = new Timer(20, 60);
timer.display();
Line 20: undefined
1: You don't set the storage as you are not calling the method that does it. either call timer.setMinutesSecondes() before calling display or do so in the constructor as per my example below.
2: It's seconds not secondes (sorry for the pedantry).
3: Your getMinutesSecondes function has 2 return calls. Execution will stop after the first call. see my example below.
4: Some of the mistakes here indicate you would benefit from some introduction to JavaScript courses. Have a quick google, there is a wealth of free content online.
class Timer {
constructor(minutes, secondes){
this.minutes = minutes;
this.secondes = secondes;
this.minutesEltHtml = document.getElementById("minutes");
this.secondesEltHtml = document.getElementById("secondes");
this.setMinutesSecondes();
}
setMinutesSecondes(){
sessionStorage.setItem("minutes", this.minutes);
sessionStorage.setItem("secondes", this.secondes);
}
getMinutesSecondes(){
return { // you were calling return twice... only the first line would have returned
mintues: sessionStorage.getItem("minutes"),
secondes: sessionStorage.getItem("secondes")
};
}
display(){
console.log(this.getMinutesSecondes());
}
}
let timer = new Timer(20, 60);
timer.display(); // output {minutes:20, secondes:60}
The reason you are getting undefined as the return value of sessionStorage.getItem is because the value you are trying to retrieve from the storage has not be set on that storage. You defined a method to store the data to the storage without calling that method ( setMinutesSecondes ).
Do this instead ( Assuming the code you presented is your complete code )
class Timer {
constructor(minutes, secondes){
this.minutes = minutes;
this.secondes = secondes;
this.minutesEltHtml = document.getElementById("minutes");
this.secondesEltHtml = document.getElementById("secondes");
}
setMinutesSecondes(){
sessionStorage.setItem("minutes", this.minutes);
sessionStorage.setItem("secondes", this.secondes);
}
getMinutesSecondes(){
return {
minutes: sessionStorage.getItem("minutes"),
secondes: sessionStorage.getItem("secondes");
};
}
display(){
console.log(timer.getMinutesSecondes());
//console.log(timer.this.minutes);
}
}
let timer = new Timer(20, 60);
timer.setMinutesSecondes(); // if you don't wish to call this method here, you can still call this method in the constructor function
timer.display();
How to check if store is open or closed based on opening time, closing time and timezone. Currently I have the code which am using in server side to check if store is still open or closed but I have been trying to make the same function in javascript or jQuery with no luck.
function OperationHours($open, $close, $timezone = "GMT"){
$status = 'closed';
$nowTime = new DateTime("NOW", new DateTimeZone($timezone));
$openTime = DateTime::createFromFormat('h:iA', $open, new DateTimeZone($timezone));
$closeTime = DateTime::createFromFormat('h:iA', $close, new DateTimeZone($timezone));
// check if the close time is before the opening time
if($closeTime <= $openTime){
$closeTime->add(new DateInterval("P1D"));
}
if ($nowTime > $openTime && $nowTime < $closeTime){
$status = 'open';
}
return $status;
}
echo OperationHours("7:00AM", "10:30PM", "Asia/Kuala_Lumpur");
I'd recommend using moment-timezone for this.
You should be able to do something like this:
const isOpen = (openTime, closeTime, timezone) => {
const now = moment().tz(timezone);
const storeOpenTime = moment.tz(openTime, "h:mmA", timezone);
const storeCloseTime = moment.tz(closeTime, "h:mmA", timezone);
return now.isBetween(storeOpenTime, storeCloseTime);
}
console.log(isOpen("7:00AM", "10:30PM", "Asia/Kuala_Lumpur"))
I'm trying to load data from multiple spreadsheets(~100) into a single spreadsheet, however when I try to do this my script times out. It appears that opening each spreadsheet takes a long time. Is there any way I can speed this up or a work around?
Here's what I use to open each spreadsheet
// We set the current spreadsheet to master and get the current date.
var master = SpreadsheetApp.getActive();
var masterSheet = master.getSheetByName('Master');
var users = master.getEditors();
var today = new Date();
// Adds the menu to the spreadsheet
function onOpen() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Update Data",
functionName : "retrievePartnerData"
}];
spreadsheet.addMenu("Submissions Menu", entries);
};
// First we get all data from the partner sheets
function retrievePartnerData() {
masterSheet.getRange(2, 1, masterSheet.getLastRow(), masterSheet.getLastColumn()).clear(); //Clear our master sheet aka Sheet All
masterSheet.hideSheet();
//Get's Promo Outline from the internal sheet and store it's values in the promoRange array
var promoRange = master.getSheetByName("Promotional Outline").getRange("A1:Z100").getValues();
var sheetPartnerArray = [];
// Row is an array that contaings the url's to the external spreadsheets
var row = master.getSheetByName('Partner Sheet Collection').getRange("B:B").getValues();
row.map(function(e){
if(e[0] != "" && e[0] != "Url"){
var ss = SpreadsheetApp.openByUrl(e[0]);
var studioName = ss.getSheets()[0].getRange("A1").getValue();
//Updates the Promotional Outline sheet in the partner sheet
var promoSheet = ss.getSheetByName("Promotional Outline");
promoSheet.getRange("A1:Z100").setValues(promoRange);
//Hide columns K to Z
promoSheet.hideColumns(11,4);
var sheet = ss.getSheets();
sheet.map(function(f){
var sheetName = f.getSheetName(); // Retrieves the sheetname of each sheet
var lastRow = 0;
if(f.getLastRow() == 1) {
lastRow = 1;
} else {
lastRow = f.getLastRow() - 1;
}
var dataRange = f.getRange(2, 1, lastRow, f.getLastColumn());
var data = dataRange.getValues();
for (var j = 0; j < data.length; j++) {
if (data[j][0].length != 0 && (data[j][5] > today || data[j][5] == "[Please Enter]")) { // We check if the promo end date is after the current day
var sheetRow = data[j];
sheetRow[1] = studioName;
sheetRow.unshift(sheetName); //Adds the Country to the beginning of the row using the sheet name from spreadsheets
sheetPartnerArray.push(sheetRow);
}
}
})
}
})
masterSheet.getRange(2, 1, sheetPartnerArray.length , sheetPartnerArray[0].length ).setValues(sheetPartnerArray);
};
Thanks!
One common approach is to set a trigger to restart your Big Job at some time in the future (just beyond the maximum execution time). Then your Big Job does as much as it can (or stops nicely at some logical point), and either gets killed or quietly exits. Either way, it gets restarted shortly after, and resumes its work.
Patt0 has taken this idea to an elegant end, providing a library that you can add to your script. With a few adaptations, you should be able to turn your retrievePartnerData() into a batch job.
Since you have a menu already, and retrievePartnerData() involves iterating over many spreadsheets, you have the opportunity to break the barrier another way, by completing each iteration (or better, a set of iterations) in a separate server script instance.
This technique appears in What happens when I "sleep" in GAS ? (execution time limit workaround)
And there is something similar in How to poll a Google Doc from an add-on. In that answer, a UI client uses a timer to repeatedly execute a server function. Here, though, iterations would be work-based, rather than time-based. This client-side function, running in your browser, would keep calling the server until there was no more work to be done:
/**
* Call the server-side 'serverProcess' function until there's no more work.
*/
function dispatchWork(){
if (window.runningProcess) {
}
google.script.run
.withSuccessHandler( //<<<< if last call was good
// After each interval, decide what to do next
function(workis) {
if (!workis.done) { //<<<<< check if we're done
// There's more work to do, keep going.
dispatchWork();
}
else {
// All done. Stop timer
stopTimer();
$('#start-process').hide();
$("#final").html(' <h2>Processing complete!</h2>');
}
})
.withFailureHandler(
function(msg, element) { //<<<<<< do this if error
showError(msg, $('#button-bar'));
element.disabled = false;
})
.serverProcess(); //<<<<< call server function
};
In your case, you first need to refactor retrievePartnerData() so it can be called from a client to process a single spreadsheet (or set of them). No doubt you have put considerable time into making that map loop work cleanly, and taking it apart will be painful, but it will be worth it.
The following spreadsheet-bound script can be adapted to your use. It consists of a menu item, a simple UI, and scripts on the client (Javascript + jQuery) and server (Google Apps Script), which control the work in intervals.
The control data is in a "SourceSheets" tab, and results will be copied to "Master".
Code.gs
var properties = PropertiesService.getScriptProperties();
// Adds the menu to the spreadsheet
function onOpen() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Update Data",
functionName : "updateData"
}];
spreadsheet.addMenu("Big Job", entries);
};
/**
* Presents UI to user.
*/
function updateData () {
var userInterface = HtmlService.createHtmlOutputFromFile("Conductor")
.setHeight(150)
.setWidth(250)
.setTitle("What 5 minute limit?");
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.show(userInterface)
}
/**
* Called from client, this function performs the server work in
* intervals. It will exit when processing time has exceeded MAX_INTERVAL,
* 3.5 minutes. Every time this function exits, the client is provided
* with the current status object, done=true when the work queue has
* been emptied.
*
* #returns {Object} Status { done: boolean }
*/
function serverProcess() {
var MAX_INTERVAL = (3.5 * 60); // minutes * seconds
var intervalStart = Math.round(new Date() / 1000);
// Get persisted work queue, if there is one
var queueProp = properties.getProperty('work-queue') || '[]';
var queue = JSON.parse(queueProp);
if (queue.length == 0) {
queue = prepareWork();
}
// Do the work for this interval, until we're out of time
while ((Math.round(new Date() / 1000) - intervalStart) < MAX_INTERVAL) {
if (queue.length > 0) {
var ssID = queue.shift();
processSheet(ssID);
properties.setProperty('work-queue', JSON.stringify(queue));
}
else break;
}
// Report result of this interval to client
var result = { done : (queue.length == 0) };
return( result );
}
/**
* Set up work queue & clear Master sheet, ready to import data from source sheets.
*
* #return {String[]} work queue
*/
function prepareWork() {
// No work yet, so set up work
var ss = SpreadsheetApp.getActive();
var masterSheet = ss.getSheetByName('Master');
var rowsToDelete = masterSheet.getMaxRows()-1;
if (rowsToDelete)
masterSheet.deleteRows(2, rowsToDelete); //Clear our master sheet aka Sheet All
// Build work queue
var queue = [];
var data = ss.getSheetByName('SourceSheets') // get all data
.getDataRange().getValues();
var headers = data.splice(0,1)[0]; // take headers off it
var ssIDcol = headers.indexOf('Spreadsheet ID'); // find column with work
for (var i=0; i<data.length; i++) {
queue.push(data[i][ssIDcol]); // queue up the work
}
// Persist the work queue as a scriptProperty
properties.setProperty('work-queue', JSON.stringify(queue));
return queue;
}
/**
* Do whatever work item we need. In this example, we'll import all data from
* the source sheet and append it to our Master.
*
* #param {String} ssID Source spreadsheet ID
*/
function processSheet(ssID) {
var masterSheet = SpreadsheetApp.getActive().getSheetByName('Master');
var sourceSheet = SpreadsheetApp.openById(ssID).getSheetByName('Sheet1');
Utilities.sleep(60000); // You probably don't want to do this... just wasting time.
var masterLastRow = masterSheet.getLastRow();
var sourceRows = sourceSheet.getLastRow();
masterSheet.insertRowsAfter(masterSheet.getLastRow(), sourceSheet.getLastRow());
var sourceData = sourceSheet.getDataRange().getValues().slice(1);
var destRange = masterSheet.getRange(masterLastRow+1, 1, sourceData.length, sourceData[0].length);
destRange.setValues(sourceData);
}
Conductor.html
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons.css">
<!-- The CSS package above applies Google styling to buttons and other elements. -->
<div id="form-div" class="sidebar branding-below">
<span id="final"></span>
<form>
<div class="block" id="button-bar">
<button class="blue" id="start-process">Start processing</button>
</div>
</form>
</div>
<div class="bottom">
Elapsed processing time: <span id="elapsed">--:--:--</span>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script>
/**
* On document load, assign click handlers to button(s), add
* elements that should start hidden (avoids "flashing"), and
* start polling for document selections.
*/
$(function() {
// assign click handler(s)
$('#start-process').click(startProcess);
});
/**
* Call the server-side 'serverProcess' function until there's no more work.
*/
function dispatchWork(){
if (window.runningProcess) {
}
google.script.run
.withSuccessHandler(
// After each interval, decide what to do next
function(workis) {
if (!workis.done) {
// There's more work to do, keep going.
dispatchWork();
}
else {
// All done. Stop timer
stopTimer();
$('#start-process').hide();
$("#final").html(' <h2>Processing complete!</h2>');
}
})
.withFailureHandler(
function(msg, element) {
showError(msg, $('#button-bar'));
element.disabled = false;
})
.serverProcess();
};
/**
* Runs a server-side function to retrieve the currently
* selected text.
*/
function startProcess() {
this.disabled = true; // Disable the button
$('#error').remove(); // Clear previous error messages, if any
startTimer(); // Start a work timer, for display to user
window.runningProcess = true;
dispatchWork(); // Start our work on the server
}
// Timer adapted from http://codingforums.com/javascript-programming/159873-displaying-elapsed-time.html
/**
* Kicks off the tick function.
*/
function startTimer( )
{
window.seconds = null;
window.ticker = null;
window.seconds = -1;
window.ticker = setInterval(tick, 1000);
tick( );
}
/**
* Stop ticking
*/
function stopTimer()
{
clearInterval(window.ticker);
}
/*
* Updates the timer display, between sleeps.
*/
function tick( )
{
++window.seconds;
var secs = window.seconds;
var hrs = Math.floor( secs / 3600 );
secs %= 3600;
var mns = Math.floor( secs / 60 );
secs %= 60;
var pretty = ( hrs < 10 ? "0" : "" ) + hrs
+ ":" + ( mns < 10 ? "0" : "" ) + mns
+ ":" + ( secs < 10 ? "0" : "" ) + secs;
$("#elapsed").text(pretty);
}
/**
* Inserts a div that contains an error message after a given element.
*
* #param msg The error message to display.
* #param element The element after which to display the error.
*/
function showError(msg, element) {
var div = $('<div id="error" class="error">' + msg + '</div>');
$(element).after(div);
}
</script>
I have the following to try to reload on a connection drop:
setInterval(window.location.reload(), 1000);
My concern with this is that it could continue forever, ddos'ing my application.
How can I update the above to try at max 20 times before giving up and breaking?
Thank you
This makes me feel dirty, but you could update/extract the window hash with each refresh:
function hack () {
var last = parseInt(location.hash.slice(1));
if (last < 20) {
window.location.hash = last + 1;
window.location.reload();
}
}
window.location.hash = 0;
setTimeout(hack, 1000);
You need to persist some counter state from one page load to the next so you can know when 20 reloads have been done. Your options are:
A hash value
A query parameter
A cookie value
Something stored in local storage
If you don't need this value to persist beyond just the reloads of this page, then options 1) and 2) are better as they are only as persistent as you need. A hash value will not be sent to your server, but could interfere with other uses of the hash value. A query parameter would be sent to the server, but any reasonable server will ignore query values it doesn't know and it won't interfere with anything else. I'd probably pick a query parameter and have actually used one to avoid infinite redirection loops in some of my code. You could implement option 2) like this:
function checkAutoReload() {
var currentCnt = 0;
var re = /(\?|&)(reloadCnt=)(\d+)/;
var param = window.location.search.match(re), newURL;
if (param) {
currentCnt = parseInt(param[3], 10);
newURL = window.location.href.replace(re, "$1$2" + (currentCnt + 1))
} else {
newURL = window.location.href;
newURL += window.location.search ? "&" : "?";
newURL += "reloadCnt=1";
}
if (currentCnt < 20) {
window.location.replace(newURL);
}
}
setTimeout(checkAutoReload, 1000);
Notice, there's no need for a setInterval() because a given page's code only runs once before it either reloads or finds that it is done reloading.
Store the reloadCount in localStorage
MDN DOM Storage
var maxReload = 20;
var reloadPage = function() {
if (typeof a !== "undefined" && a !== null) {
console.log(localStorage.reloadCount);
localStorage.reloadCount = 0;
};
var reloadCount = parseInt(localStorage.reloadCount, 10);
console.log(reloadCount);
if (reloadCount < maxReload) {
reloadCount += 1;
localStorage.reloadCount = reloadCount;
// RELOAD CODE HERE
};
};
// call reloadPage from your code
reloadPage();