I have a note in the right sidedbar that embeds sections from another note based on time of day, using dataviewjs. I use this for time-blocking purposes. I need this to be a subtle visual effect rather than, for example, a disruptive pop-up notification, which is why I’ve gone down this route. Here’s a simplified version of the code:
function getTimeBlock() {
const currentMinute = new Date().getMinutes();
if (currentMinute < 45) {
if (currentMinute < 43) {
return "![[TimeBlocksFile.md#day1]]" + "![[TimeBlocksFile.md#day2]]";
}
return "![[TimeBlocksFile.md#day1]]";
}
return "![[TimeBlocksFile.md#evening2]]";
}
dv.span(getTimeBlock());
What I’m trying to do
The code works as intended, but the sidebar note only reloads the result if I start manually editing the code block (cursor must be inside the block and I also have to input something, like an extra space). When the note is in the main workspace, it updates when I navigate away from it and come back. Neither result is ideal and I wonder if there’s a way to make this run without my interaction.
Things I have tried
For context, I’m not a programmer so I just approach things based on a hunch, trying to apply methods that seem applicable to my layperson brain.
I tried adding the following at the end of my code, without success:
setInterval(getTimeBlock, 1000);
I changed how I wrote the if statements (to what I posted here) and caught and fixed an error too, but these fixes aren’t giving different results.
I also tried setInterval(getTimeBlock(), 1000);
which didn’t help but makes the console throw this error in a loop (a):
Uncaught error: unexpected identifier ‘#evening2’
I then asked a piece of AI and it suggested to toggle the note’s edit mode on and off to make it reload. It gave this code:
setInterval(() => {
this.app.workspace.activeLeaf.view.sourceMode.toggle();
this.app.workspace.activeLeaf.view.sourceMode.toggle();
}, 1000);
…to which console responds:
Uncaught TypeError: this.app.workspace.activeLeaf.view.sourceMode.toggle is not a function
I decided to not try to fix that because even if I managed to programmatically toggle the source/preview modes, I’m not confident that it would help, as doing the same manually won’t update the file in the sidebar, which is the main feature I’m after.
As far as I can tell, any code in a note is performed one time only. I couldn’t find much information on this forum or the Obsidian documentation, but then again, I don’t know the correct terminology for what I’m looking for, so I may have missed something. Any help would be appreciated!