views:

83

answers:

4
$(document).ready(function() {
    $(".rshownews").click(function() {
        window.setInterval(function() {ajaxselectrss($(this).attr("title"))}, 1000);
    });
});

 function ajaxselectrss(rssurlvar) {
  var ajaxRequest;  // The variable that makes Ajax possible!

 try{
  // Opera 8.0+, Firefox, Safari
  ajaxRequest = new XMLHttpRequest();
 } catch (e){
  // Internet Explorer Browsers
  try{
   ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
   try{
    ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
   } catch (e){
    // Something went wrong
    alert("Your browser broke!");
    return false;
   }
  }
 }
 // Create a function that will receive data sent from the server
 ajaxRequest.onreadystatechange = function(){
  if(ajaxRequest.readyState == 4){

   var ajaxDisplay = document.getElementById('news');
   ajaxDisplay.innerHTML = ajaxRequest.responseText;
  }
 }



 //var rssurlvar = $(this).attr("title");
 var queryString = "rurl=" + rssurlvar;
 var urltofile = "rssget.php";
 ajaxRequest.open("POST", urltofile, true);
 ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
 ajaxRequest.setRequestHeader("Content-length", queryString.length);
 ajaxRequest.setRequestHeader("Connection", "close");
 ajaxRequest.send(queryString); 

}

But the POST query is undefined.Why?

+1  A: 

maybe you should use setInterval rather than setTimeout

Christian Smorra
+5  A: 

Your code should look like this at least:

$(document).ready(function() {
 $(".rshownews").click(ajaxselectrss);
});

function ajaxselectrss() {
  //ajax function
}

setTimeout(ajaxselectrss, 1000);

For repeated task, use setInterval instead.

Sarfraz
The same.I edited my post.
lam3r4370
@lam: remove quotes from `'someFunc($(this).attr("title"))'`, try `someFunc($(this).attr("title"))`
Sarfraz
useless setInterval call (missing quotes around argument?)
lam3r4370
Dont remove the double quotes from `"title"` just the single quotes wrapping your function.
Sarfraz
@Sarfraz - unless someFunc returns a function, removing the quotes won't help. You need to pass a function to setInterval, not the result of one.
sje397
+1  A: 

You cant use $(this) in a setInterval, maybe something like this will work

var rshownews;
function someFunc(rssurlvar) {
  $('body').append('<br>'+rssurlvar);
}
$(document).ready(function() {
    $(".rshownews").click(function() {
        rshownews = $(this).attr("title");
        window.setInterval('someFunc(rshownews)',1000);
    });
});​

Edit with setInterval you need the var and function to be in the global scope, demo

"someFunc is not defined"
lam3r4370
code updated, you needed the function `someFunc` and var `rshownews` in the global scope
The same.I edited my post with the whole code.
lam3r4370
+1  A: 

setInterval can take a string or a function pointer. Passing a string containing '$(this)' won't work (because passing a string has the effect of the string being eval'd at call time).

So try:

var title = $(this).attr('title');
setInterval(function() {someFunc(title);}, 1000);
sje397