How to store javascript code in database? [closed] - javascript

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.

Related

React execute a function at varying time intervals [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 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.

is rendering html using js a bad practice? [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 6 years ago.
Improve this question
Please ignore my syntax.
This is just an example if this is a bad idea.
let's say I have select with 50 options but instead of typing out options 50 times.
would it be a good idea to use js and give it an array variable.
run through the variable.length then append it into html.
Is it bad to use js like this though and why?
No issues. Now clients are way too much powerful than they were earlier before.
So, utilizing some of its power never harms.
Now the problem .. rendering template on client is not an issue ..Angular, React all does the same.
While you are doing only a little which may consumes less than < 1 ms. So , just go ahead.
it is a better practice.
You are a programmer, you will never need to type out options 50 times :)

Write to file in javascript? [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 6 years ago.
Improve this question
I have made a simple site that generates random number.
I want to record how many times a specific number comes up.
Is there anyway I can use javascript to write to a Local .txt File on the server?
or do I have to learn PHP?
If you want to keep track of the number of times a random number comes up for a single client/browser, you can use localStorage. If you want to keep track of the number of times the random number occurs across all executions, you'll need some sort of server-side processing.
If you want to avoid any server side processing and this data is critical then you can try GoogleAnalytics code to capture and analyaze the randomly generated data.

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 do I create a simple javascript server? [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 8 years ago.
Improve this question
I want to create a server where if I pressed "w", a variable (lets say me1) would equal to 1. And if another user (from a different browser and IP) pressed the same key, another variable (lets say other2) would equal to 1.
I don't need everything to be JavaScript (as long as it's easy to understand), I just need the end variables to be JavaScript.
Do your users from different IP's need to be able to talk to each other?
You could use NodeJS as one way to do this. I only recommend Node because its very simple to setup sockets if your answer to my above question is yes.
However both technologies would require a medium to advanced skill set.

Categories