Solved

Need help troubleshooting javascript for keeping PHP session alive using ajax

Posted on 2013-11-19
9
839 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 58

Expert Comment

by:Gary
ID: 39660644
ahh ignore me I missed you were calling the function again.
0
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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)

740 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