React execute a function at varying time intervals [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
I have an API which returns a list of objects. Each of these objects have a field called timeOut which contains some time interval in secs. Once the time interval of any object expires, some operation needs to performed on that particular object.
How can I achieve this in react ? I considered using setInterval on each object in the list, but it looks like a bad design.
Any help will be appreciated. Thank you.

You should use setTimeout instead of setInterval if you want an operation to run only once after the given time period. I believe this is your best bet to accomplish this task.
Otherwise, you will have to dive into even crazier designs like comparing the present date-time object from new Date() with the one you generated right after fetching your data, which will not only be complicated but also, more error-prone and inaccurate.
Let me know if you want help with the first method.

Related

Javascript Variable Naming For Loop Problem [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 days ago.
Improve this question
Can someone help me figure out why naming a variable changes the random value so much? I'm stumped.
In the first picture, you can see that the maximum value of the X value will be a random value between 25 and 300. However, EVER TIME I run the code, the lines are super short.
When I make a top_limit variable, shown in the second picture, the lines have true random lengths.
Can someone shed some light on what's happening here? There's no difference between the two pieces of code, in my opinion. One just has a variable name and the other doesn't. No?
When your random is in a for(,,), it is called on every iteration of the loop. So, in each iteration it changes.

Update value of nested array in firebase database [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm building a web app with Vue.js using Firebase authentication and real time database, which I'm new to.
In this app the user answers to questions to achieve some points.
My problem is: I want to update an existing array already stored and created in the database so that the progress/points of that user will be under track to update all the components in my app.
For example let's say I want to increase +1 the highlighted value in the screenshot:
I was thinking about something like userid.progress[6][0] += 1 like in normal javascript, but I can't update the database, value still at 0.
I've read something about .set() or .update() but in that case I should rewrite all the array (and it would be a problem), but I might be completely wrong.
PS: it's not a problem about getting the user id or the current user, everything is working fine.
Hope to find someone to help me, thank you so much in advance!
If you're trying to update an existing score, you can write on the most granular level. So:
ref.child("pjoCy...0tl3/progress/0/6/0").set(1)
If you want to push a new score to the end of the array, you'll indeed need to read the entire score array to add a new value to the end. This is one of the many reasons why Firebase recommends against using array indices like you do. If you instead us so-called push-IDs, adding a new value becomes as easy as:
ref.child("pjoCy...0tl3/progress/0/6").push(1)
I recommend reading more about structuring lists in the Firebase documentation and the classic blog posts on handling arrays in Firebase.

How to store javascript code in database? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I created a webshop for an online game where the users can buy in-game stuff. I have some special items which will be only availabe for a short period of time and I want to print out how much time left to buy it.
I wrote a JS code to countdown for a date, and now I want to store it in the database. Which is the best way to do it? Just a varchart with extremely long length? Or is it a more elegant way to deal with this?
You can use Function.prototype.toString method to convert a function into a string and then later use eval to execute the function.
You should use text type rather than varchar as it will likely be longer than maximum varchar length.
However, I would strongly discourage you to do this. A better approach would be to store something like a expiration date timestamp and calculate the availability based on that value.

Convert Time to UTC [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a timepicker that allows the user to select a time. If they select 7:00pm how can I convert that to UTC 24H time?
If there is already an answer to this question and someone could direct me to it that would be great.
Since you tagged JavaScript and jQuery, I could guess you are using https://github.com/jonthornton/jquery-timepicker#timepicker-plugin-for-jquery? If so, try calling getTime method:
$('#my-timeicker-id').getTime(); // returns Javascript Date object
Check for your timepicker's API (e.g. https://github.com/jonthornton/jquery-timepicker#methods) for more.
--
In case you are using your own timepicker, you will have to implement it yourself. For learning purposes great, go for it! Otherwise don't reinvent the wheel and pick an existing solution.

How to implement a count-down Timer on a Web Site [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to get an website element that is always changing, such as a countdown timer that resets when someone clicks on a button. Anyone know how I can implement a constantly changing element in my C# program?
You can implement periodic updates using client-site Javascript functions, either setInterval() or setTimeout(), with syntax like following:
// start digital clock
function StartTopClock() { _pID = window.setInterval('UpdateTopClock()', 100);}
which provides periodic updates every 100 ms.
Detailed description is available at: http://digiclock.codeplex.com/, working demo at: http://webinfocentral.com/
Hope this will help. Best regards,

Categories