Changing dynamic page view through another page

I want to change the dynamic page (the page each viewer is seeing on my site) through another page (an admin panel which I have created). I have used a check function on the dynamic page, which is supposed to check a field value every 1 second and then I have a code which uses switch code for different values.

Through the admin panel, in which I have buttons, I am changing the status field of the dataset. This change is reflected in the database, but the dynamic page is not changing automatically. I have to refresh the page in order to see the change, which then works fine. But I want it to automatically check the status every second and then change accordingly.

I am using console.log to debug and it is showing me the status value every second but the code related to when the status value is changed is not working without refresh.

Please can somebody help me. I am stuck.

You will need to supply more information for us to be able to assist you.

What you are trying to do. What code do you have? What works? What doesn’t?

I have made an admin panel to change the value of the field status in the database. I have trimmed the code to only show the relevant portion. Ignore the number variable as that is used to store some other value in another field.

export async function startbutton_click(event) {
 number = 0;
 await $w("#statuslist").setFieldValues({
 "status": "Stop",
 "number": number
    });
    $w('#statuslist').save();
    
export async function stopbutton_click(event) {
 number = number + 1;
 await $w("#statuslist").setFieldValues({
 "status": "Stop",
 "number": number
    });
    $w('#statuslist').save();

This is the view panel on the dynamic page on which the user is present.

$w.onReady(function () {

        vartimer = setInterval(() => checkstatus(), 1000);
    }

);

async function checkstatus() {
    status = $w('#mainlist').getCurrentItem().status;
    console.log(status);

 switch (status) {
 case "Start":
 await start();
 break;
 case "Stop":
 await stop();
 break;
    }
}

function start() {
    $w('#notification').text = "Event has started"
}

function stop() {
    $w('#notification').text = "Event has stopped"
}

The text should change from start to stop automatically as the code is looking for status value each second. I can see the change reflected in the database but not on the page. If the user side page is refreshed, then it shows the changed text.

Both mainlist and statuslist link to the same database.

Can somebody please help?

It’s really not clear what you are trying to accomplish.

On the admin panel code, is statuslist a dataset ? You say you want to " change the value of the field status in the database". Does the dataset have a current item? Is it the same item that’s being used on the dynamic page?

It may be that your pages aren’t using the same database item.

Also, you don’t need await when calling start() and stop() as they are not asynchronous functions.

Yes, statuslist is a dataset. I have a button on admin panel to change the status field of current item to “start” or “stop”. And based on that status, the text on the dynamic page should change because it is checking that field every second.

The changing of field part is working as I’m using the console log to capture value of status on the page and it’s coming out to be correct.

But the text is not changing without refreshing the page. I want it to change automatically as soon as the value in status field changes.

Thanks.

I suspect that the dataset’s item doesn’t get updated after the dynamic page is loaded. Not sure, but I think you need a way to refresh the dataset, or to reload the data.

You could do a query() with code and that should get you the updated values.

But when I check the console log it is giving me the updated value of status value every second. So that should not be the issue.

:exploding_head: Can’t believe I didn’t see it before, but your startbutton_click() function sets the status to Stop . I’m gonna scream.

Oh sorry. That’s a typo. When I tried to copy and trim the code. On the real site it’s correct.

I am able to see the log on the dynamic page correctly, i.e. it spells out start and stop every second based on the value…but the text I want to change doesn’t work.

Not sure what’s going on, but you have to understand that the getCurrentItem() doesn’t return a Promise since it just gets the current dataset item. However, if the data were to change in the collection, it would require another db query to get the new value.

I would suggest trying a dataset refresh() and see if that helps:

$w('#mainlist').refresh();
let status = $w('#mainlist').getCurrentItem().status;

Also, on the line of code await start(); the await is not necessary and might cause a problem.

Thanks. The refresh was what was missing. Now its working as intended.
The start function has more code inside it which I removed before posting here so it was necessary.

A follow up query. Would there be a problem if multiple people will try to edit a dataset or a data collection at the same time or Wix is able to manage that?

Thanks again for the help !