setTimeout not executing
setTimeout not executing
我的setTimeout函数不想工作。我没有收到任何警报,所以我认为我做了一些非常愚蠢的事情。
var timeout = 1000;
for(k=0;k<pages.length;k++)
{
randomnumber=Math.floor(Math.random()*pages[k].length);
setTimeout(function() {
$.ajax({
type: 'GET',
url: pages[k][randomnumber],
success: function(data) {
alert(data);
if(data.indexOf('VIDEO_LENGTH') > 0)
{
timeouttext = data.substr(data.indexOf('VIDEO_LENGTH')+12);
timeouttext = timeouttext.substr(timeouttext.indexOf('.'));
timeout = parseInt(timeouttext);
alert(timeout);
}
else
timeout = 1000;
$('#loader').hide('fast','fade');
$('#information').html(data);
$('#information').show('fast','fade');
}
})
},timeout);
alert("PAGE " + k + " RandomNumber " + randomnumber + " : " + pages[k][randomnumber]);
if(k==3) {
k = 0;
}
}
有人能帮我吗?
编辑:ok跟随错误:
调用页面url时的vark与预期不同。所以试试:
{
randomnumber=Math.floor(Math.random()*pages[k].length);
var selectedIndex = k; // store current k
setTimeout(function() {
$.ajax({
type: 'GET',
url: pages[selectedIndex][randomnumber], // use stored k
...
当您使用FireBug或CHrome的开发工具调试代码时,您看到Ajax请求了吗?
在我看来,你从来没有为每个请求获得"成功"事件。由于Ajax请求中没有"错误"事件,因此您永远不会看到任何反馈。
您的setTimeout是正确的。要进一步检查错误:
-
在控制台中检查您的请求是否正在发送。
-
添加记录请求状态的
error: function(){}
或complete:function(){}
块,查看服务器端是否破坏了代码。
edit:(我建议你不要使用缩写if
,它很容易出错。
在这里试试这个(更新)小提琴。
var funfacts= ['URL1','URL2','URL3'];
var multimedia= ['URL4','URL5'];
var employees = ['URL6','URL7','URL8','URL9','URL10'];
var information = ['URL11','URL12','URL13'];
var pages = [funfacts, multimedia, employees, information];
var timeout=1000;
var t=0;
for(i=0;i<pages.length;i++)
{
(function(value){
for(j=0;j<pages[value].length;j++)
{
t++;
(function(iv, ij){
var tmo = timeout*t;
setTimeout(function(){
callAjax(iv, ij);
}, tmo);
})(value, j);
}
})(i);
}
function callAjax(i, j)
{
$.ajax({
type: 'GET',
url: pages[i][j],
success: function(data) {
if(data.indexOf('VIDEO_LENGTH') > 0) // indexOf should be checked if(var.indexOf('sometext')!=-1)
{
// your code
}
else
{
// your code
}
}
});
}
如果其他一切都很好,那么它应该起作用。
你也可以在SO上阅读这个和这个。
相关文章:
- jQuery is not loaded
- AngularJS JSON not arriving php
- $rootScope not working
- reactRedux is not defined
- jquery ajax success functions not executing
- rails jquery ajax request not executing
- Javascript not executing google chrome extensioin
- SoundCloud JS SDK get handler not executing
- Office 365 on iPad - SP.SOD.executeFunc not executing functi
- Webview javascript not executing
- Rails 3.2.2 not executing rjs
- setTimeout not executing
- jQuery .post() not executing
- DataTables Ajax not executing
- Cordova / Phonegap -- jquery not executing Javascript on <
- Angularjs $timeout not executing
- Delphi, EmbeddedWB/TWebbrowser - jQuery not executing
- JS not executing
- Jquery else not executing
- e.preventDefault not executing