Solved

Need help troubleshooting javascript for keeping PHP session alive using ajax

Posted on 2013-11-19
9
828 Views
Last Modified: 2013-11-20
I have the following javascript that doesn't appear to be doing anything. It;s supposed to make an ajax request and display an alert messagee very 5 seconds, .. but it doesn't appear to be working at all:

<script type="text/javascript">
var refreshSn = function () {	
	var time = 5000; // 5 secs
    settimeout(
        function ()
        {
        $.ajax({
           url: '../user/refresh_session.php',
           cache: false,
           complete: function () {refreshSn();}
        });
		alert('Session Re-started!');
    },
    time
);
};
</script>

Open in new window


The answer is probably simple, .. but I'm drawing a blank.  Can anyone here maybe spot what might be wrong with the script? It's not even throwing any errors.  I just never see the expected alert message.

Thanks,
- Yvan
0
Comment
Question by:egoselfaxis
9 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39660587
setInterval
setTimeOut is a one off command.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39660590
Are you loading jQuery before that?  You are using jQuery syntax.
0
 

Author Comment

by:egoselfaxis
ID: 39660600
Gary .. I'm not sure I understand your comment.  
Are you saying that it's impossible for this script to work?

And yes Dave .. I am loading jquery before that.

- Yvan
0
 
LVL 58

Expert Comment

by:Gary
ID: 39660644
ahh ignore me I missed you were calling the function again.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 58

Expert Comment

by:Gary
ID: 39660651
URL should be a proper url, this will not work as their is no relative path as far as jquery is concerned.

url: '../user/refresh_session.php',

It should be the full url (domain) or the path in relation to the root.
0
 

Author Comment

by:egoselfaxis
ID: 39660672
No, you're mistaken.  All of my other ajax functions within the same page are using relative paths and are all functioning just fine.

- Yvan
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39660785
Change your code to this

<script>
var time = 5000; // 5 secs
(function refreshSn(){
   setTimeout(function(){
       $.ajax({
	url: '../user/refresh_session.php',
	cache: false,
	complete: function(){
	refreshSn ();
	alert('Session Re-started!');
           },
 });
 }, time);
})();
</script>

Open in new window

0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39661019
@egoselfaxis - nowhere in your code are you actually calling your function. You define it, but never call it!! Call it when the document is ready:

$(document).ready(function() {
    refreshSn();
});

Open in new window

Also, javascript is case sensitive - settimeout() is wrong. It should be setTimeout()
0
 

Author Closing Comment

by:egoselfaxis
ID: 39662393
Thank you!  It works now!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now