简体   繁体   中英

setTimeout and click

$(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?

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.

也许您应该使用setInterval而不是setTimeout

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM