简体   繁体   English

如何停止调用新功能的Ajax图像加载?

[英]How to stop Ajax image load calling a new function?

I'm doing something like this -> 我正在做这样的事情->

$("<img>").attr("src", "path/to/image.jpg").load(function(){
    $("#thediv").append(this);
});

so i can append the new image when the image is completely loaded 这样我就可以在图像完全加载后附加新图像

I have a timer to call this function with new parameters every 6 seconds.... but I want to call a function to load a new set of images but I have this old set running in background ( asynchronous ) and i need to stop that load so the image won't append beacuse i don't want that image and I'm trying to load a whole new different set of images... I was thinking on something like : 我有一个计时器,每6秒钟用新参数调用此函数。...但是我想调用一个函数以加载一组新图像,但是我有这个旧组在后台运行(异步),因此我需要停止该操作加载,因此图像将不会追加,因为我不想要该图像,而我正在尝试加载一组全新的不同图像集...我在考虑以下内容:

function loadNewSet(){
   window.stop(); // i don't quite know if this exist or works but 
                 //should stop all ajax request taking place at that time
   loadImages(); // call some function to load the new set
}

/* / /*/ / /*/ / /*/ / /*/ / /*/ / /*/ / * / / * / / / * / / / * / / / * / / / * / / / * /

To be more specific: 更加具体:

I have a div called thediv where I'll be placing my images 我有一个名为thediv的div,我将在其中放置图片

then I have set of images or an array 然后我有一组图像或一个数组

set1 = ['img1','img2','img3']

and

set2 = ['img4','img5','img6']

and i have thetimer set on the 我在上面设置了thetimer

$(document).ready(function(){
    thetimer=setTimeout("nextSlide()",6000);
});

then i have nextSlide() 然后我有nextSlide()

function nextSlide(){
     //this is where i load the next image on the active set (set 1 or set 2)
    img.load(function(){
       thetimer=setTimeout("nextSlide()",6000); // and then i set the timer again
    });
}

but when the nextSlide() function is called it'd be like idle while the new image is loading and after it loads it'd the things it's supposed to do on load() but what it while loading i call this function loadNewSet() 但是,当调用nextSlide()函数时,它就像是在加载新图像时一样是空闲的,而在加载新图像后,它应该在load()上执行该操作,但是在加载时我将其称为该函数loadNewSet()

function loadNewSet(set){
    clearTimeout(thetimer);
    //this is wheere i want to stop any image loading called by any other function
    stopAllImagesLoading();//example
    //then change activeset
    activeSet=set; // set1 or set2
    nextSlide();
}

Don't quite know if I'm doing this the right way and may be I can't put down the procces here the way I'm doing it. 不太清楚我是否以正确的方式进行操作,可能是我无法按照自己的方式在此处放置程序。

Thanks for your responses. 感谢您的回复。

You can spawn up "worker processes" by using setTimeout. 您可以使用setTimeout生成“工作进程”。

var proc;
somefunc();

function somefunc() {
    //do stuff
    proc = setTimeout(somefunc, 6000);
}

//...

clearTimeout(proc); //Cancel the looping somefunc from executing.

This should help you: Kill ajax requests using javascript using jquery. 这应该可以帮助您: 使用jquery使用javascript杀死ajax请求。

Just keep all your requests in an array. 只需将所有请求保存在一个数组中即可。 When you want to stop them all, loop though them and call .abort() on each. 当您要停止所有它们时,遍历它们并.abort()对它们调用.abort()

What if instead of appending, you do something like.... 如果您执行类似...而不是追加操作,该怎么办?

<div id="myDiv">
    <img id="myImg" style="display: none">
</div>

... ...

function loadImg(url){
    $("#myImg").attr("src", url).load(function(){
        if(url == $(this).attr(url)){
            // The currently requested image has loaded.  Show it.
            $(this).show();
        }
        else{
            // Finished loading an image that we don't need anymore.  Do nothing.
        }
    });
}

So if you called: 因此,如果您致电:

loadImg("a.jpg");
loadImg("b.jpg");

a.jpg might or might not finish first, but it doesn't matter because once a.jpg finishes, nothing happens. a.jpg可能会先完成,也可能不会先完成,但这并不重要,因为一旦a.jpg完成,就什么也不会发生。

Try using a global variable to turn on or off the timer. 尝试使用全局变量打开或关闭计时器。 For instance (pseudo-code only): 例如(仅伪代码):

var gKeepRunning = true;   //KEY TO ANSWER HERE

myRepeatingImageLoader();   //this is the method that will repeat

function myRepeatingImageLoader(){
  $("<img>").attr("src", "path/to/image.jpg").load(function(){
    $("#thediv").append(this);
  });

  if( gKeepRunning == true )   //KEY TO ANSWER HERE - WILL STOP WHEN SET TO FALSE
    var junk = setTimeout('myRepeatingImageLoader();', 6000);   //Repeat again
}

function loadNewSet(){
  gKeepRunning == false;   //KEY TO ANSWER HERE
  loadImages();
}

Found the anwser here: Javascript: Cancel/Stop Image Requests 在此处找到答案: Javascript:取消/停止图像请求

    if(window.stop !== undefined)
    {
         window.stop();
    }
    else if(document.execCommand !== undefined)
    {
         document.execCommand("Stop", false);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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