I'm building a website and i want to implement a reminder feature that alerts people if they have any projects or activities that are due or simply alert them of any timer they may have set. I have a clue about how i could do it with javascript's timer functions but i want it to run all the time and add reminders to a queue if the users are not online when the event occurs. Is there any way of doing this or do i have to use bash or python on the server?
Further explanation on how it's supposed to work:
- infinite loop checks for the time every X seconds, if there's a a reminder up between now and X seconds ago, print it to the user it belongs to - if the user is not online, put it in a file or something which is checked, when the user logs in, for any missed reminders.another way i thought of is to use a local script (pyton or something) to check the database for reminders that are due, every X seconds, and write them in a file on the web server. then, server-side scripting will read it every X seconds and print the reminders to each l开发者_开发百科ogged account (and delete it at that point). This way no reminders are skipped even if the person it belongs to is logged out.
Any idea on how to do this more elegantly?
Use Cron Job instead and run your script with that to connect to your database and to do other tasks.
this functionality can be done with ajax and php. I wouldn't check the db when the user isn't online. I guess it is better to check reminders on login (unless you want to for example email them when they occur->then you need cron like mentioned in the other answers). you'll need to make a php script that checks which reminders have to be set and return them in an array/json. on login call this page. when user is logged in you can request your reminder php periodically/timer with ajax.
Having an infinite loop is by default bad design. PHP won't like it at all (memory). Javascript isn't ideal for the same reason too. Apart from that you don't want a javascript sleep to block your UI when waiting between tries.
Also, I wouldn't check notices when the user is not online. This is a useless way of using resources. Simply save the last time a user was online and display past notices that he hasn't seen before on login.
The cleanest solution in my book is some javascript library capable of eventdriven actions. An example of that is node.js. With events you don't need to check every N time but simply have an event triggered if some condition is met.
精彩评论