Get the PC date from an app in JavaScript? - javascript

After trying many googlings I can not find any thing related to this problem. What I wanted is getting my PC's date not the Google's server date.
I did try by using URLFetchApp to make a HTTP request with a hope that some JavaScript can be executed and then I can have a nice PC's date as the response but it was not so simple. I do not really have no idea how to make some progress right now so may you can give some hints?

Sure. You're going to need this:
(1) Set up a URI on the app side to retrieve the data
(2) use a tiny bit of javascript in your client side to capture Date, and translate it to JSON.
(3) Send that JSON to the URI.
This will be a lot easier if you can make the client initiate it, say at the time your page loads, but it can be done by having the server side request it.
Details of how to handle this depend somewhat on what Javascript library etc you use. I like jQuery, which would make the second step something like jQuery.toJSON(new Date()).

Related

architecture for get and store api request data

This is more of a architectural questions. An external platform had product and price information for let's say, books. There is an API available to get this information.
What I read is that it should be possible to create a function in Javascript and connect the Javascript to a page where you want to show the data on my own website. This would mean that for each page request an API-call is made. Since the requested information only changes once a day maximum this does not sound the most efficient solution.
Can someone advise a better solution? Something into the direction of a similar php or javascript function that does the request on the background, schedule an update and import the data into mysql? If so, what language would be most common.
I need the solution for a Joomla/php/mysql environment
Here's a simple idea - fetch and store results from the API (ones you think aren't gonna change in a day), either on disk, or in the database, and later use these stored results to retrieve what you otherwise would've fetched from the API.
Since storing anything in frontend JS across page reloads isn't easy, you need to make use of PHP for that. Based on what's given, you seem to have two ways of calling the API:
via the frontend JS (no-go)
via your PHP backend (good-to-go)
Now, you need to make sure your results are synced every (say) 24 hours.
Add a snippet to your PHP code that contains a variable $lastUpdated (or something similar), and assign it the "static" value of the current time (NOT using time()). Now, add a couple of statements to update the stored results if the current time is at least 24 hours greater than $lastUpdated, followed by updating $lastUpdated to current time.
This should give you what you need with one API call per day.
PS: I'm not an expert in PHP, but you can surely figure out the datetime stuff.
It sounds like you need a cache, and you're not the first person to run into that problem - so you probably don't need to reinvent the wheel and build your own.
Look into something like Redis. There's an article on it available here as well: https://www.compose.com/articles/api-caching-with-redis-and-nodejs/

Using HTML5 how can i send a simple string so I can see it?

So I have a game written in HTML5 which is all fine and dandy. At the end of the game there is a score the player recieves. I want my game to send me how many people have a score of atleast 100 or above. I need this statistic to balance out the game accordingly.
I was thinking I could make a txt file in a dropbox account and make the game edit the file.
Now pls dont tell me about security issues I am aware that they can change their score in the javascript console which sends false data.
How do I go about solving this simple task? I just need a way to tell how many people are getting a score of 100 or above from my game.
Thanks !!!
You will probably need to use a server-side language such as PHP to write to a remote file.
You will want to javascript something simple that when the the score is >100 (or whatever your threshold is) you would do a simple (probably ajax) query to a server.
While plenty of people are telling you to create a server-side app, if you do not already have a server, there are simpler ways to go. For example I believe google spreadsheets has an api or even better you could use something like Parse Data which is a free database-as-a-service with a simple javascript API and intended just for that purpose.
On the other hand, if you DO have a server but don't feel like setting up a full programming language to respond to API calls (which is NOT terribly hard by the way), you could use the server log. Whenever the user hits 100 points for example create a little invisible <img src="http://yoursite.com/i-got-over-100" height=1 width=1 style="position: absolute, left: -10000" /> then just check your server logs for how much i-got-over-100 was hit.

avoid mismatch between server side and client side result

I use PHP and Javascript. In my website some results are processed server side some client side.
Using javascript only, prevents your website from being crawled correctly by search engines and using PHP only prevents correct real time response.
The problem is how to grantee both js functions and PHP functions give the same result? for example suppose there is a function which gives relative time:
JS:
function relative_time(timestamp)
{
...
}
PHP:
function relative_time($timestamp)
{
...
}
Keeping both functions matched with each other is not easy since I want to edit both. For example if both give us:
one year ago
And I change PHP only, to give me:
a year ago
Then JS is not updated too. Is there any standard way to ensure both will act in the same way?
unfortunately js function cannot be called on server side.
If there is some complicated logic, you should implement it server-side and just pull the results via AJAX. That way you only need to maintain the PHP code and provide a kind of AJAX API for access via JS.
I think you just need to make a decision where it is to be done, because if they do vary which is to be dominant ? (that's the one that should be doing it)
Also, how are you saving server load by doing it in both locations ?
Avoid this by making a decision for which code is to do it, failing that, Put a note in you code at both locations reminding yourself to update both locations ?

calculate number of working days on a rails form

I have two fields, 'from_date' and 'to_date' on my rails form. When a user enters the dates I have to display the number of working days i.e, exclude (sat, sun and a list of other official holidays in a database table).
Is this something that should be done in the client side javascript
Or is this something that should be done by making an ajax request to rails.
Can you tell me how I can accomplish this.
thanks much.
Depending on how often that feature would be used I'd opt for doing both, create a .js-file (or generate dynamically and just make sure rails caches it) and try it with javascript locally for instant gratification. Then make sure it's correct on the server side on saving.
The upside with doing it both on the client and on the server is that you may save some hassle when the guy can see it directly on the site without doing a request. But if they have javascript disabled it'll still work. And if it's anything important you should never trust what you get from a web form. :)
This patch might give you some ideas. https://rails.lighthouseapp.com/projects/8994/tickets/2304-add-concept-of-weekdays
You might be interested in business_time.

Ajax "Is there new content? If so, update page" - How to do this without breaking the server?

It's a simple case of a javascript that continuously asks "are there yet?" Like a four year old on a car drive.. But, much like parents, if you do this too often or, with too many kids at once, the server will buckle under pressure..
How do you solve the issue of having a webpage that looks for new content in the order of every 5 seconds and that allows for a larger number of visitors?
stackoverflow does it some way, don't know how though.
The more standard way would indeed be the javascript that looks for new content every few seconds.
A more advanced way would use a push-like technique, by using Comet techniques (long-polling and such). There's a lot of interesting stuff under that link.
I'm still waiting for a good opportunity to use it myself...
Oh, and here's a link from stackoverflow about it:
Is there some way to PUSH data from web server to browser?
In Java I used Ajax library (DWR) using Comet technology - I think you should search for library in PHP using it.
The idea is that server is sending one very long Http response and when it has something to send to the client it ends it and send new response with updated data.
Using it client doens't have to ping server every x seconds to get new data - I think it could help you.
You could make the poll time variable depending on the number of clients. Using your metaphor, the kid asks "Are we there yet?" and the driver responds "No, but maybe in an hour". Thankfully, Javascript isn't a stubborn kid so you can be sure he won't bug you until then.
You could consider polling every 5 seconds to start with, but after a while start to increase the poll interval time - perhaps up to some upper limit (1 minute, 5 minute - whatever seems optimal for your usage). The increase doesn't have to be linear.
A more sophisticated spin (which could incorporate monzee's suggestion to vary by number of clients), would be to allow the server to dictate the interval before next poll. The server could then increase the intervale over time, and you can even change the algorithm on the fly, or in response to network load.
You could take a look at the 'Twisted' framework in python. It's event-driven network programming framework that might satisfy what you are looking for. It can be used to push messages from the server.
Perhaps you can send a query to a real simple script, that doesn't need to make a real db-query, but only uses a simple timestamp to tell if there is anything new.
And then, if the answer is true, you can do a real query, where the server has to do real work !-)
I would have a single instance calling the DB and if a newer timestamp exists, put that new timestamp in a application variable. Then let all sessions check against that application variable. Or something like that. That way only one innstance are calling the sql-server and the number of clients does'nt matter.
I havent tried this and its just the first idéa on the top of the head but I think that cashe the timestamp and let the clients check the cashe is a way to do it, and how to implement the cashe (sql-server-cashe, application variable and so on) I dont know whats best.
Regarding how SO does it, note that it doesn't check for new answers continuously, only when you're typing into the "Your Answer" box.
The key then, is to first do a computationally cheap operation to weed out common "no update needed" cases (e.g., entering a new answer or checking a timestamp) before initiating a more expensive process to actually retrieve any changes.
Alternately, depending on your application, you may be able to resolve this by optimizing your change-publishing mechanism. For example, perhaps it might be feasible for changes (or summaries of them) to be put onto an RSS feed and have clients watch the feed instead of the real application. We can assume that this would be fairly efficient, as it's exactly the sort of thing RSS is designed and optimized for, plus it would have the additional benefit of making your application much more interoperable with the rest of the world at little or no cost to you.
I believe the approach shd be based on a combination of server-side sockets and client-side ajax/comet. Like:
Assume a chat application with several logged on users, and that each of them is listening via a slow-load AJAX call to the server-side listener script.
Whatever browser gets the just-entered data submits it to the server with an ajax call to a writer script. That server updates the database (or storage system) and posts a sockets write to noted listener script. The latter then gets the fresh data and posts it back to the client browser.
Now I haven't yet written this, and right now I dunno whether/how the browser limit of two concurrent connections screws up the above logic.
Will appreciate hearing fm anyone with thoughts here.
AS

Categories