• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 874
  • Last Modified:

Need help troubleshooting javascript for keeping PHP session alive using ajax

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
egoselfaxis
Asked:
egoselfaxis
1 Solution
 
GaryCommented:
setInterval
setTimeOut is a one off command.
0
 
Dave BaldwinFixer of ProblemsCommented:
Are you loading jQuery before that?  You are using jQuery syntax.
0
 
egoselfaxisAuthor Commented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
GaryCommented:
ahh ignore me I missed you were calling the function again.
0
 
GaryCommented:
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
 
egoselfaxisAuthor Commented:
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
 
GaryCommented:
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
 
Chris StanyonCommented:
@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
 
egoselfaxisAuthor Commented:
Thank you!  It works now!
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now