Time check function in extjs? - javascript

I'm trying to make an email form with extjs 4 that sends an email to 12000 company members at a time.
The server wouldnt hold that large number at one time, so im trying to write a function that generates the sending method every three messages or something.
Does anybody know of a suitable function? Sample code? Or a way to refresh an extjs form every few seconds/minutes/etc?

Not sure that's the best approach. But for refreshing a page it's basic javascript
document.location.reload(true)
and use a setTimeout to repeat the reload.
So a reload afther 5 seconds:
var t = setTimeout(function(){document.location.reload(true)}, 5000);
But I rather advice using an ajax call and a setInterval to repeat every x ms so you wouldn't have to reload any page at all!
An even cleaner approach is to handle it on the backend.
Another helpful article
Another helpful article

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/

Is it safe to call jQuery ajax every 1 second

I have php file, that generates json with a countdown seconds to the New Year eve and to some birthdays. But I want the countdown to change every one second. Is it safe (will it overload the server) if I setTimeout(1000) to my ajax call function?
And what's the best way to implement 1 second JSON call via jQuery?
Thank you
Instead of overloading your server with requests (take into context you might have many users doing many requests at once coupled to your constants callbacks), take your data all at once for the following day for example and process the birthdays only in javascript.
The only Ajax portion i'd use in there would be to load more birthdays once per hour or day in case you have a really hyped user that leave his browser open.
The load on the server depends on:
The amount of work the php does each time it is called
and
The number of users you expect
Depending on what the server is doing, you may well be able to move the countdown logic into the client javascript, with the server just calculating the initial values. You can then use a timer in javascript to update every second, calculating the difference between the current time and the starting values.
Depends on your server and how optimised your PHP endpoint is... its a very conditional question.
However for what your doing, I would also suggest doing it all from JS.. even with birthdays, I would set a JSON object in the markup with all the birthdays that are recorded, then you can do it all client side... maybe call ajax every 5-10 minutes for any new birthdays that may be added... depends on what your application is.
I used setInterval, and checked if the remaining time equals zero. Then I rerun the ajax function.
thank you, all :) you were helpful after all ;)
if I understand, you want to call your server every second to update your time.
Can't you just :
Call your server to get the actual time
List item 2 - Process with a JS setInterval(functionIncrementAndUpdateDate, 1000); ?
You could use Keith Wood's countdown timer: http://keith-wood.name/countdown.html
It is extremely easy to use and no need for AJAX. All you have to do is include the plugin file and write:
<div id="timer"></div>
$(document).ready(function() {
$('#timer').countdown({
until: '<?php echo date("h:i:s"); ?>' // change this to New Year or a Birthday from DB
});
});
Demo: http://jsfiddle.net/tqyj4/436/

How can I make a click counter in dreamweaver in javascript?

I'm creating a website, that has a button, I want it to work in such way that whenever someone clicks it a number is increased by one, I want this number not to be reset when the page is refreshed or accessed from somewhere else.
Please help me with a sample code... Im a beginner
As mentioned by #SLaks, you will need some method of storing this counter on the server - otherwise there is no way for a page request to know how many times it has been called before. To do this you will need both:
Server-side code to read the value and display it to the user, and also to update the value when the page is requested the next time, and
Server-side storage to actually hold the counter value.
One possible solution to this (without adding a huge amount of additional complexity) would be to use PHP to read/write the counter, and a simple text file to hold the value. There are other ways, but may make things a bit too hard (i.e. no need for a database to hold one value).

Live updating charts in google app engine (python)

I want the graph to update after every specific interval of time (say 10 seconds). During this time the app would either do processing to determine the upcoming value of graph (which would include acquiring information from certain web-pages using urlfetch) or remain idle.
Is it possible to make such a graph in GAE? If so I'll highly appreciate if someone can guide me to the right direction. Also how would the normal 1 minute deadline per app request apply here (and basically how to avoid it? since i would like the graph to remain updating for some time say 2-5 minutes..)
Two thinks to do:
Do the data processing on server: use Cron to invoke your data-processing code. Requests invoked by Cron have 10min deadline.
Client side reload - there are many ways to do it:
a. The most simple way - make HTML page reload itself at regular intervals:
<META HTTP-EQUIV="refresh" CONTENT="15">
b. Use javascript timer to update only a part of page:
setTimeout("javascript statement to update part of page",milliseconds);
c. Use Channels API, so that data-processing task, when finished, notifies clients to reload the page.
Options a. and b. are simple to implement, but they just blindly update the page even if there is no new data to display. They also update at regular intervals, instead of when there is new data to display. You can use one of those if there is only a few clients using this page.
Options c. it the hardest to implement, but is IMO the right way, since it only reloads the page when there is actually something new to display. You should use this option if you have a lot of clients using this page.

How are timed html forms, like the kind you encounter when doing online tests, properly coded using PHP, MySQL, HTML, CSS and JavaScript?

In other words, what technology would take care of doing the time tracking? Would it be the JavaScript? I don't see being able to keep track of such things with PHP.
Here's what I need to accomplish:
I need to have a long form spanning many web page reloads because it is pretty much an online test where each page load displays a new question. However, the entire form, which constitutes one test has a time limit. When the time expires if the user in question has not completed the test then he/she cannot submit a partially completed test nor attempt to do the entire test all over again within the same day (either calendar date or with 12/24 hours in between.) The user may, however, comeback the next day and attempt to finish the entire test again within the allotted time. I know I have added a lot of details and I did this just to show context, nevertheless, the main difficulty for me would be in how accomplish the time expiry feature. That is, somehow, within a series of pages that make up a form representing an online test I want to track the time starting from the first question (one page load) and upon time expiry for the test to be disabled.
Has anyone ever done this? Would anyone have any tips for me on how to accomplish this?
Any advice I can get would totally be appreciated in advance.
If you do track time on client-side - always validate it on the server-side.
Never trust the client, by itself, to validate the time. As mentioned in the comments, client-side time validation is only good for cosmetic features, never for actual validation.
The easiest way to accomplish this is to add a unique token to the form (which is not spoofable) on first navigation. Cookies, or any other sort of session management technique you get from your framework will suffice.
On form submission you can first validate this on client side and return an error if time has passed, even before actually sending the form. If successful, submit the form, and make sure you validate the token upon processing on the server.
There are two ways you could measure the "time they spent" on the form.
When the first page of the form is severed, in the PHP create a session variable containing the date. When they finish the form, you subtract the current date form the beginning date. This gives you the total time it took from when the form was served and finished. However, this is not 100% accurate as there could downtime for other reasons such as slow internet.
You could have JavaScript record the time on that page (I answered how to do that here: here) Using AJAX, this time could be sent that way or by using GET posts. The time would then be kept by PHP somehow and added up in the end.
Hope this helps! Just ask if you want an example.
In the most general terms, you'll need to set up a session on the server side to track each user and test. When the user begins the test, you stamp a variable (server side) with the test start time. As the user progresses through the test and requests additional pages, check whether the difference between the current time and that variable have exceeded the time allowed for the test. If the test has expired, instead of delivering the next test page, you can close up the test and deliver a "time's up" page to the user.
I don't know what server-side environment you're using, but it almost certainly has some sort of session management framework available. To reliably control the testing environment, you have to manage this from the server side.

Categories