Solved

Need help troubleshooting javascript for keeping PHP session alive using ajax

Posted on 2013-11-19
9
825 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 82

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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 42

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
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 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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

706 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

12 Experts available now in Live!

Get 1:1 Help Now