07-29-2021 09:04
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-29-2021 09:04
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
In my clockface index.js I am registering this event
Answered! Go to the Best Answer.
Accepted Solutions
08-02-2021 14:15 - edited 08-02-2021 14:18
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

08-02-2021 14:15 - edited 08-02-2021 14:18
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
@mkostersitzI think I see your problem.
document.addEventListener("activate", (evt) => {
loadCurrentState("app activate");
});
I've never seen EventListeners used for `document`. Normally for HTML elements that you pull in like you did here
let appVersionLabel = document.getElementById("app-version");
From your original post, you said you want it to run everytime the clock loads (i.e. returning from another app)? If I'm not mistaken, you could do:
import * as document from "document";
import clock from "clock";
import * as userActivity from "user-activity";
import * as powerUtil from "./battery.js";
import * as heartMonitor from "./hrm.js";
import { me as appbit } from "appbit";
import * as weather from "./weather.js";
import * as fs from "fs";
import { launchApp as launchApp} from "system";
import * as logger from "../common/logger.js";
import * as cache from "./cacheManager.js";
import * as global from "../common/globals.js";
import * as timeDate from "./timeDate.js";
import { units as units } from "user-settings";
import * as messaging from "messaging";
import * as util from "../common/utils";
import { memory as memory } from "system";
import { vibration as vibration} from "haptics";
// Register for the unload event
appbit.onunload = saveCurrentState("app unload");
// Register for activation event
loadCurrentState("app activate");
// version
let appVersionLabel = document.getElementById("app-version");
if(global.settings.isDebug) {appVersionLabel.text = global.settings.appVersion;}
I believe this should give you what you are wanting.
07-29-2021 11:55 - edited 07-29-2021 12:13
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-29-2021 11:55 - edited 07-29-2021 12:13
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
First thing that comes to mind is this: Is the event listener on the highest level possible (same level as your imports)?
For example:
import clock from "clock";
import document from "document";
.
.
.
// Functions and whatnot
.
.
.
// Register for activation event
document.addEventListener("activate", (evt) => {
loadCurrentState("app activate");
});

07-30-2021 09:49
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-30-2021 09:49
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
it is right after the imports in my code.

07-30-2021 10:40 - edited 07-30-2021 10:43
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-30-2021 10:40 - edited 07-30-2021 10:43
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
@mkostersitz Alright, would you mind sharing from the beginning of your imports to the end of the event listener in question then?
I have a suspicion as to what the problem is, but seeing the code up til that part would confirm

08-02-2021 14:08
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

08-02-2021 14:08
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
Sorry for the delay here it is
import * as document from "document";
import clock from "clock";
import * as userActivity from "user-activity";
import * as powerUtil from "./battery.js";
import * as heartMonitor from "./hrm.js";
import { me as appbit } from "appbit";
import * as weather from "./weather.js";
import * as fs from "fs";
import { launchApp as launchApp} from "system";
import * as logger from "../common/logger.js";
import * as cache from "./cacheManager.js";
import * as global from "../common/globals.js";
import * as timeDate from "./timeDate.js";
import { units as units } from "user-settings";
import * as messaging from "messaging";
import * as util from "../common/utils";
import { memory as memory } from "system";
import { vibration as vibration} from "haptics";
// Register for the unload event
appbit.onunload = saveCurrentState("app unload");
// Register for activation event
document.addEventListener("activate", (evt) => {
loadCurrentState("app activate");
});
// version
let appVersionLabel = document.getElementById("app-version");
if(global.settings.isDebug) {appVersionLabel.text = global.settings.appVersion;}
08-02-2021 14:15
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


08-02-2021 14:15
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
appbit.onunload = saveCurrentState("app unload")
This causes saveCurrentState to be executed immediately, and whatever it returns is then assigned to onunload. Presumably saveCurrentState returns nothing (ie, undefined), so that's what gets assigned to onunload. This means that no handler (listener) is being set.
You probably want something like
appbit.onunload = saveCurrentState
which will assign the function to onunload, rather than what the function returns.
f() causes f to be executed; f without () is a reference or link to the function which can be executed subsequently.
Gondwana Software
08-02-2021 14:15 - edited 08-02-2021 14:18
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

08-02-2021 14:15 - edited 08-02-2021 14:18
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
@mkostersitzI think I see your problem.
document.addEventListener("activate", (evt) => {
loadCurrentState("app activate");
});
I've never seen EventListeners used for `document`. Normally for HTML elements that you pull in like you did here
let appVersionLabel = document.getElementById("app-version");
From your original post, you said you want it to run everytime the clock loads (i.e. returning from another app)? If I'm not mistaken, you could do:
import * as document from "document";
import clock from "clock";
import * as userActivity from "user-activity";
import * as powerUtil from "./battery.js";
import * as heartMonitor from "./hrm.js";
import { me as appbit } from "appbit";
import * as weather from "./weather.js";
import * as fs from "fs";
import { launchApp as launchApp} from "system";
import * as logger from "../common/logger.js";
import * as cache from "./cacheManager.js";
import * as global from "../common/globals.js";
import * as timeDate from "./timeDate.js";
import { units as units } from "user-settings";
import * as messaging from "messaging";
import * as util from "../common/utils";
import { memory as memory } from "system";
import { vibration as vibration} from "haptics";
// Register for the unload event
appbit.onunload = saveCurrentState("app unload");
// Register for activation event
loadCurrentState("app activate");
// version
let appVersionLabel = document.getElementById("app-version");
if(global.settings.isDebug) {appVersionLabel.text = global.settings.appVersion;}
I believe this should give you what you are wanting.
08-02-2021 14:19
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

08-02-2021 14:19
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
08-02-2021 14:21
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


08-02-2021 14:21
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
No worries; I'm glad we got there. 🙂
Gondwana Software
08-02-2021 14:23
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

08-02-2021 14:23
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Glad to help! Happy coding!

