Solved

Need help troubleshooting javascript for keeping PHP session alive using ajax

Posted on 2013-11-19
9
850 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
How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

 
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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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…
Suggested Courses

636 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