Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Jquery中加载图像和错误行为_Javascript_Jquery_Load - Fatal编程技术网

Javascript 在Jquery中加载图像和错误行为

Javascript 在Jquery中加载图像和错误行为,javascript,jquery,load,Javascript,Jquery,Load,我对自制剧本有点小问题,首先我给你剧本 var heighti = $(window).height(); var imageLoading = new Array(); $('.fullHeight').css({'height' : heighti}); var now,hour,minute,second,dateNow = new Array(),countImg=0,i=0,countDateNow = 0;countImg=0,countThis=0,countDat

我对自制剧本有点小问题,首先我给你剧本

    var heighti = $(window).height();
var imageLoading = new Array();
$('.fullHeight').css({'height' : heighti});
    var now,hour,minute,second,dateNow = new Array(),countImg=0,i=0,countDateNow = 0;countImg=0,countThis=0,countDateNowAj=0;
        /* GET THIS HOUR */
        now = new Date();
        hour      = now.getHours();
        minute    = now.getMinutes();
        second    = now.getSeconds();
    function date(){
        //Function to get date
}
function loadImage(){
    countThis = 0;
    while(countThis < 6){
    date();
    var imgOn = 'uploads/original/'+dateNow[countDateNowAj]+'.jpg';
    console.log(imgOn);
    var img = $("<img />").attr('src', imgOn)
                  .load(function(){
                    imageLoading[i] = imgOn ;
                    i++;
                  })
                  .error(function(){
                  console.log('This is the image now : '+imgOn);
                    imageLoading[i] = 'images/noimage.jpg';
                    i++;
                  });
        countThis++;
        countDateNowAj++;
    }
}


setInterval("dateImg()",1000);
setTimeout("loadImage()",0);
setInterval("loadImage()",5000);
var heighti=$(window.height();
var imageload=新数组();
$('fullHeight').css({'height':heighti});
var now,hour,minute,second,dateNow=new Array(),countImg=0,i=0,countDateNow=0;countImg=0,countThis=0,countDateNowAj=0;
/*在这个时间*/
现在=新日期();
hour=now.getHours();
分钟=现在。getMinutes();
second=now.getSeconds();
功能日期(){
//获取日期的函数
}
函数loadImage(){
countThis=0;
while(countThis<6){
日期();
var imgOn='uploads/original/'+dateNow[countDateNowAj]+'.jpg';
console.log(imgOn);
变量img=$(“
这是我的函数,一切都正常,但当我想做
imageload[I]=imgOn;
脚本总是取最后一个值

这是我所说的一个日志:

首先,我检查时间
在我检查图像后,将检查谁
最后,我检查了
imageload[I]=imgOn;

我总是得到最后一次,而不是每一秒


我希望您能理解我的查询。

在循环外声明imageLoading,如下所示

var imageLoading = [];

在load和error handler函数中,您正在异步使用外部作用域中的变量(在本例中,作用域将是loadImage函数),但它们将作为循环的一部分进行同步更改。如果希望在实际调用处理程序之前保持不变,则需要使用闭包:

function loadImage(){
   function imageLoader(i, imgOn) {
      console.log(imgOn);
      var img = $("<img />").attr('src', imgOn)
         .load(function(){
            imageLoading[i] = imgOn ;
         })
         .error(function(){
            console.log('This is the image now : '+imgOn);
            imageLoading[i] = 'images/noimage.jpg';
         });
   }

   for(countThis = 0; countThis < 6; countThis++, countDateNowAj++) {
      date();
      imageLoader(i++, 'uploads/original/'+dateNow[countDateNowAj]+'.jpg');
   }
}
函数loadImage(){
函数imageLoader(i,imgOn){
console.log(imgOn);
变量img=$(“

在这种情况下,imageLoader内部函数成为load和error处理程序运行的作用域,因此i和imgOn的值与您期望的一样,而不是始终是循环运行结束时的最后一个值。

我这样做了,在我的脚本顶部,这是什么,在哪里声明为空?它在sec上声明第二行。如果您愿意,我可以给您发送脚本?我很感兴趣,请继续。我不明白…似乎您没有正确复制/粘贴。您的date()函数为空,dateNow[]将永远不会更改等。