The title basically sums up the issue that I’m attempting to address. I’ve been playing around with some of the features but can’t quite figure out how to design a function that will allow my button to be disabled after pressed, but become available again at ex. 5am. The idea is that this button can only be pressed once per day. I appreciate any insight as I couldn’t solve this by searching the forum myself and I’m sure this will help others as well
It looks like this can be circumvented by simply closing a re-opening the app. Unless there’s a way to have this running in the background that I’m not aware of?
@anush_bh20910 That will allow the button to be clicked if and only if the hour is 5. But I think @earthquack28ur actually wants the button enabled at 5am and after that. So for example, if I pressed the button at 8pm, I couldn’t press it again until 5am the next day. But I could press it at 6am or 10am or 1pm or…
There’s no way to run code in the background in a Thunkable app. But you can definitely check the time each time the screen is opened and as @catsarisky suggested, save the current state of the button (as a variable value) in a stored variable.
@tatiang is correct. I’ve been trying to come up with a function that tests the current time as well as the seconds since 1970 and comparing it to the last stored variable to indicate that it is after 5am of the following day. I just can’t quite figure out how to format the function to do so. I like the idea of storing that as a variable so that it can be compared every time the app is opened. Ultimately I’m going to need a similar function to process once a week, but that will be far more complex.
When button is clicked
If device month ≥ stored month AND device day > stored day AND device hour ≥ 5
then
[do whatever actions you want]
set stored month to current month
set stored day to current day
It would be trickier to enable/disable the button because you’d have to check the date & time constantly or at least every so often.
Also, my suggestion probably needs adjusting. For example, if you get the month and it’s 12 and then the next month is 1, my If condition isn’t going to work correctly. Same with end of month days like April 30 to May 1.
So maybe comparing “seconds since 1970” to a stored value and a combination of the above?
This is basically what I’ve come up with. I’ve done some testing by adjusting my phone time ahead and it appears to work. My guess is that I can do the same thing for the weekly function by converting the day of the week to a time and adding it to the equation.
We really need blocks intended to work with time. Like, blocks based on the moment.js or luxon.js libraries
Any ideas if this will ever happen @domhnallohanlon ? Using the seconds since 1970 block is useful, but parsing a human readable timestamp into seconds and vice versa is not easy with thunkable. I will be at this is the most effective way to work with time in my opinion.
Here’s a thread on it! Essentially the benefit here would be the ability to turn second since 1970 into a human readable timestamp and vice versa, turn a human readable timestamp into a second since 1970. That makes it a whole lot easier to do date math. It’s a whole ID easier to add 86,400 seconds to the previous number than it is one calendar day and to figure out manually with the next calendar day and month are going to be. Currently I uncle user has to figure those things out manually and that’s just not necessary currently with the given JavaScript architecture that’s available.
In addition it allows you to return a wide range of human readable timestamps from the second since 1970. It also allows you to easily parse it into any time zone you need it to be in. This comes in handy when you’re doing things like Making a scheduling app
Why would you want to link any library to use a function that is natively in JS.
You can use something like this. Remember JS uses the time upto the millisecond so you need to multiply the data number by 1000 to use it in JS and then use the date() function to get the required date format.
Yes @jared having date/time blocks in Thunkable is essential. Reaching out to web viewer and back is not a solution and has its own drawbacks but it is now a workaround other than relying on APIs till the blocks are available.
Just tell me example of what?
I do many things in JavaScript and combine it with my Thunkable apps. I sent you the link for getting next / previous day using JS in another post.
Hello @muneer ,
Thanks I saw your exemple. I will work on it on Thursday and see if I have any questions. I want to understand how it works and see how can I use it