[英]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? 但是POST查询是不确定的,为什么?
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
。
也许您应该使用setInterval而不是setTimeout
You cant use $(this)
in a setInterval, maybe something like this will work 您不能在setInterval中使用
$(this)
,也许类似的东西会起作用
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进行编辑,您需要将var和function置于全局范围内, 演示
setInterval
can take a string or a function pointer. setInterval
可以使用字符串或函数指针。 Passing a string containing '$(this)' won't work (because passing a string has the effect of the string being eval'd at call time). 传递包含'$(this)'的字符串将不起作用(因为传递字符串会导致在调用时评估该字符串)。
So try: 因此,请尝试:
var title = $(this).attr('title');
setInterval(function() {someFunc(title);}, 1000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.