JQuery wheel spin onload - php

I have little problem with JQuery.
Am making spinning wheel application. That application start when I click on button.
I want to change that, because my client will determine when wheel to begin.
Most of my configuration is stored in a database but I try with single number 1 = start, 0 stop.
My app have this for button click and start:
this.cache.wheelSpinBtn = $('.wheel');
// test
this.cache.wheelSpin = 1; // 1 auto start 0 False
Application start when i press button with class .wheel
this.cache.wheelSpinBtn.on('click', function (e) {
e.preventDefault();
if (!$(this).hasClass('disabled')) _this.spin();
});
But I want to remove the button from the other visitors, and this command is issued in the administration. As I said in my configuration is based
I try without database :
if(this.cache.wheelSpin === 1) {
this.spin(); // if is 1 start spinning
}
With database that looks:
if(this.cache.wheelSpin === <?php echo $obj->getStatus()?> ) {
this.spin(); // if is 1 start spinning
}
In both cases that is not work when i refresh page.
I just want when i refresh page wheel start spins. If is value 1 and my core know when need to stop.
My applucation code is little compicated and my code is in and its inposible to use .ready() on only one this.cache.wheelSpin...
Check video.
http://www.youtube.com/watch?v=H-yi6Sv71rs&feature=youtu.be

Related

How to avoid counter multiplication when user reenter the component?

I'm using angular-user-idle npm package to track browser inactive status and display a session out popup on preset timeout . The library works well when you do not redirect from that particular component on timeout. When you redirect to home page or similar one and reenter the same earlier component and if timeout happens this time, popup appear multiple time now.
Now in order to explain you this, I have displayed counter number during 1st load of component and 2nd load of the component in the screenshots below. If you carefully look at second image number of each counter is appearing twice. What is the reason for it same is resulting in popup appearing multiple times? How can I avoid it?
ngOnInit() {
//Start watching for user inactivity.
this.restart();
this.userIdle.startWatching();
// Start watching when user idle is starting.
this.userIdle.onTimerStart().subscribe(count => console.log(count));
// Start watch when time is up.
this.userIdle.onTimeout().subscribe(() => {
this.openGenericDialog('GenericAlert', "Your session is expired!");
this.dialogRef.afterClosed().subscribe(
result => {
this.dialogResult = result;
this.stopWatching()
this.stop();
this.createUpdateLoggedInRecord('Simulation',
this.varUser.UserKeyID, false);
this.router.navigateByUrl('/authentication');
});
}
);
}
stop() {
this.userIdle.stopTimer();
}
stopWatching() {
this.userIdle.stopWatching();
}
restart() {
this.userIdle.resetTimer();
}
I think you might have a memory leak because you don't keep track of your subscriptions.
this.userIdle.onTimeout().subscribe(() => {
...
^^ This.
Should be this (vv)
this.userIdle.onTimeout()
.pipe(take(1))
.subscribe(() => {
...

"Live notifications" feaure

I want to add "live notifications" feature for my users: When they receive a new message(via the chat system) then display a notification badge on the chat icon in the navbar.
I was thinking to do: AJAX calls with setInterval:(As seen on another post here)
The AJAX call:
setInterval('checkUpdates', 2000);
function checkUpdates() {
$.get('/check_updates.php?timestamp=' . lastTime, function (results){
// do stuff here
}
);
The PHP code:
$timestamp = $_REQUEST['timestamp'];
$results = query('SELECT message FROM messages WHERE messageTime > "$timestamp"');
echo fetch($results);
I have a few questions before:
1) Since I want the update-checking to be constantly running, and the navbar is always present (always on the top of the page), is the navbar.php file the correct place to put that AJAX call?
2) How badly does that affect performance of the website, to have AJAX calls constantly running every 2 seconds?
3) Is there a better way? Do websites like Facebook have an efficient way to immediately popup the chat window whenever you get a new message, or it's thanks to their huge amount of servers?
Thanks

Notification bubble on admin panel menu if new user added using php ajax?

How can i add notification bubble like Facebook in my admin menu (when new user is signup) by using Ajax php also after viewing/clicking the menu bubble disappear.Anybody help???
When a user signs up, I guess you add him/her in your database? If so, I would add a field in your users database called "notificationViewed", which would be false by default when you put that user in the database.
When you connect or refresh you admin menu page, your php that serves the page should check the database if any user has a field notificationViewed == false, and COUNT the number of such returned users. In your html tag that represents the buble, add an attribute data-newUsers="<?= COUNT_OF_NEW_USERS ?>".
Now on the client-side...
Have, let's say, id="bubble" hidden by default with CSS:
#bubble {
display:none;
}
With JavaScript, you can access the data-* attributes easily:
var newUsers = document.getElementById('bubble').dataset.newUsers; // holds the number
or with jQuery:
var newUsers = $('#bubble').data('newUsers'); // same thing
At this point, you can check if newUsers > 0. If so, populate the bubble with the number (if you want), and do a nice fadeIn animation. Example in jQuery:
if (newUsers > 0) {
$('bubble').text(newUsers).fadeIn();
}
Now, we want to be able to detect when the bubble is clicked, in order to hide the bubble and discard the new users signed up. Again, with jQuery:
$('#bubble').click(function() {
$.post('discardNotifications.php', {usersNotified: newUsers}, function(data) {
if (data === "ok") { // php script returns this string if all went right
$('#bubble').fadeOut(function() {
$('#bubble').remove(); // remove the element from the DOM, to prevent further clicks of the element
}); // nice fadeOut animation of the bubble
}
}
});
The function will only be called if the POST request was successful. The POST request is directed to discardNotifications.php, which must be in the same directory as your admin-menu html file (if not, just change the relative path). The second parameter of the call is a litteral object containing the number of new users notified, which is sent to your back-end.
Back on the back-end, inside discardNotifications.php...
You must check if there's a POST parameter called "usersNotified", then query your users database and update at most the number given by "usersNotified". This takes into account that maybe new users subscribed since you refreshed your admin page, and you want the notification of these new users. Not selecting a maximum of "usersNotified" would possibly ignore them. Example (but not complete):
if (isset($_POST['usersNotified']))
{
$number = $_POST['usersNotified'];
// update the "notificationViewed" field to TRUE for at most $number different users
echo "ok"; // everything went right
} else {
echo "bad";
}
There are obviously changes you can make, and you have to implement some of the database handling. Tell me if it works!
Ps: there might be little errors in my code snippets, I didn't test everything.

How to get dynamic home page

I have a index.html file consist of certain data with refresh button.
On pressing refresh button it will call refresh.php.
Refresh.php connects database and gets new updated data from database (Say- today's event data) And shows updated data in that refresh.php page
this is what I do.. But I want dynamic home page and want to remove refresh button. In short- whenever user loads homepage..that division should display updated data from database. So should I use .index.php and use php code in index.php itself will work?
I dont want to use asp/ajax/cookie/session. Please give me idea apart from these. Thanks :)
You could check the file's age, e.g.
<?php
$ca_file = '/path/to/foo.blah';
if (is_file($ca_file)) {
// check if file is not older then 1 hour
if (time() - filemtime($ca_file) <1 * 3600) {
$ca_news = 'y';
}
}
That would check if the file is not older then 1 hour. You'll probably want something smaller. Now all you need to do is to check the value of $ca_news and do your magic.

Curing the "Back Button Blues"

Ever stumbled on a tutorial that you feel is of great value but not quite explained properly? That's my dilemma. I know THIS TUTORIAL has some value but I just can't get it.
Where do you call each function?
Which function should be called
first and which next, and which
third?
Will all functions be called in all files in an application?
Does anyone know of a better way cure the "Back Button Blues"?
I'm wondering if this will stir some good conversation that includes the author of the article. The part I'm particularly interested in is controlling the back button in order to prevent form duplicate entries into a database when the back button is pressed. Basically, you want to control the back button by calling the following three functions during the execution of the scripts in your application. In what order exactly to call the functions (see questions above) is not clear from the tutorial.
All forwards movement is performed by
using my scriptNext function. This is
called within the current script in
order to activate the new script.
function scriptNext($script_id)
// proceed forwards to a new script
{
if (empty($script_id)) {
trigger_error("script id is not defined", E_USER_ERROR);
} // if
// get list of screens used in this session
$page_stack = $_SESSION['page_stack'];
if (in_array($script_id, $page_stack)) {
// remove this item and any following items from the stack array
do {
$last = array_pop($page_stack);
} while ($last != $script_id);
} // if
// add next script to end of array and update session data
$page_stack[] = $script_id;
$_SESSION['page_stack'] = $page_stack;
// now pass control to the designated script
$location = 'http://' .$_SERVER['HTTP_HOST'] .$script_id;
header('Location: ' .$location);
exit;
} // scriptNext
When any script has finished its
processing it terminates by calling my
scriptPrevious function. This will
drop the current script from the end
of the stack array and reactivate the
previous script in the array.
function scriptPrevious()
// go back to the previous script (as defined in PAGE_STACK)
{
// get id of current script
$script_id = $_SERVER['PHP_SELF'];
// get list of screens used in this session
$page_stack = $_SESSION['page_stack'];
if (in_array($script_id, $page_stack)) {
// remove this item and any following items from the stack array
do {
$last = array_pop($page_stack);
} while ($last != $script_id);
// update session data
$_SESSION['page_stack'] = $page_stack;
} // if
if (count($page_stack) > 0) {
$previous = array_pop($page_stack);
// reactivate previous script
$location = 'http://' .$_SERVER['HTTP_HOST'] .$previous;
} else {
// no previous scripts, so terminate session
session_unset();
session_destroy();
// revert to default start page
$location = 'http://' .$_SERVER['HTTP_HOST'] .'/index.php';
} // if
header('Location: ' .$location);
exit;
} // scriptPrevious
Whenever a script is activated, which
can be either through the scriptNext
or scriptPrevious functions, or
because of the BACK button in the
browser, it will call the following
function to verify that it is the
current script according to the
contents of the program stack and take
appropriate action if it is not.
function initSession()
// initialise session data
{
// get program stack
if (isset($_SESSION['page_stack'])) {
// use existing stack
$page_stack = $_SESSION['page_stack'];
} else {
// create new stack which starts with current script
$page_stack[] = $_SERVER['PHP_SELF'];
$_SESSION['page_stack'] = $page_stack;
} // if
// check that this script is at the end of the current stack
$actual = $_SERVER['PHP_SELF'];
$expected = $page_stack[count($page_stack)-1];
if ($expected != $actual) {
if (in_array($actual, $page_stack)) {// script is within current stack, so remove anything which follows
while ($page_stack[count($page_stack)-1] != $actual ) {
$null = array_pop($page_stack);
} // while
$_SESSION['page_stack'] = $page_stack;
} // if
// set script id to last entry in program stack
$actual = $page_stack[count($page_stack)-1];
$location = 'http://' .$_SERVER['HTTP_HOST'] .$actual;
header('Location: ' .$location);
exit;
} // if
... // continue processing
} // initSession
The action taken depends on whether
the current script exists within the
program stack or not. There are three
possibilities:
The current script is not in the $page_stack array, in which case it is
not allowed to continue. Instead it is
replaced by the script which is at the
end of the array.
The current script is in the
$page_stack array, but it is not the
last entry. In this case all
following entries in the array are
removed.
The current script is the last entry
in the $page_stack array. This is
the expected situation. Drinks all
round!
That is a good discussion but more to the point you should be looking into Post Redirect Get (PRG) also known as "Get after Post."
http://www.theserverside.com/patterns/thread.tss?thread_id=20936
If you do not understand my article then you should take a close look at figure 1 which depicts a typical scenario where a user passes through a series of screens – logon, menu, list, search, add and update. When I describe a movement of FORWARDS I mean that the current screen is suspended while a new screen is activated. This happens when the user presses a link in the current screen. When I describe a movement as BACKWARDS I mean that the user terminates the current screen (by pressing the QUIT or SUBMIT button) and returns to the previous screen, which resumes processing from where it left off. This may include incorporating any changes made in the screen which has just been terminated.
This is where maintaining a page stack which is independent of the browser history is crucial – the page stack is maintained by the application and is used to verify all requests. These may be valid as far as the browser is concerned, but may be identified by the application as invalid and dealt with accordingly.
The page stack is maintained by two functions:
scriptNext() is used to process a
FORWARDS movement, which adds a new
entry at the end of the stack and
activates the new entry.
scriptPrevious() is used to process
a BACKWARDS movement, which removes
the last entry from the stack and
re-activates the previous entry.
Now take the situation in the example where the user has navigated to page 4 of the LIST screen, gone into the ADD screen, then returned to page 5 of the LIST screen. The last action in the ADD screen was to press the SUBMIT button which used the POST method to send details to the server which were added to the database, after which it terminated automatically and returned to the LIST screen.
If you therefore press the BACK button while in page 5 of the LIST screen the browser history will generate a request for the last action on the ADD screen, which was a POST. This is a valid request as far as the browser is concerned, but is not as far as the application is concerned. How can the application decide that the request is invalid? By checking with its page stack. When the ADD screen was terminated its entry was deleted from the page stack, therefore any request for a screen which is not in the page stack can always be treated as invalid. In this case the invalid request can be redirected to the last entry in the stack.
The answers to your questions should therefore be obvious:
Q: Where do you call each function?
A: You call the scriptNext()
function when the user chooses to
navigate forwards to a new screen,
and call the scriptPrevious()
function when the user terminates
the current screen.
Q: Which function should be called
first and which next, and which
third?
A: Each function is called in
response to an action chosen by the
user, so only one function is used
at a time.
Q: Will all functions be called in
all files in an application?
A: All functions should be available
in all files in an application, but
only called when chosen by the user.
It you wish to see these ideas in action then you can download my sample application.
The part I'm particularly interested in is controlling the back button in order to prevent form duplicate entries into a database when the back button is pressed.
Your premise is wrong. There is no such thing as "Back Button Blues", if you design your application as a web application. If you design your application without any server side state, you will never run into this problem in the first case. This minimalistic approach to web applications works remarkably well, and is usually known as REST.
# troelskn
If you design your application without any server side state ....
It is not possible to design an effective application which does not have state, otherwise all you have is a collection of individual pages which do not communicate with each other. As maintaining state on the client is fraught with issues there is no effective alternative but to maintain state on the server.
#Marston.
I solved the problem with post/redirect/get but I believe the tutorial has some merit and perhaps Tony Marston can elaborate on it. And how it could be used to solve not necessarily my particular problem but perhaps something similar. Or how is it better than post/redirect/get if the functions can in fact be used in solving my particular problem. I think this will be a good addition to the community here.
if ($_POST) {
process_input($_POST);
header("Location: $_SERVER[HTTP_REFERER]");
exit;
}

Categories