Search code examples
javascriptdatabasecountdown

How to store javascript code in database?


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?


Solution

  • 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.