警报显示两次

时间:2017-05-10 18:01:53

标签: javascript cordova html-framework-7

我使用cordova和Framework7来创建应用程序。我的脚本加载并从服务器显示图像正常,但如果服务器返回404我得到警报2次。为什么,我该如何解决?

function view(a) {
var img = new Image();
var s = a;
myApp.showPreloader('Загружаем...');
img.src = s;
img.onload = function() {
    document.getElementById('showimg').innerHTML = '';
    var openPhotoSwipe = function(a) {
        myApp.hidePreloader();
        myApp.allowPanelOpen = false;
        document.getElementById("check").style.display = "block";

        var pswpElement = document.querySelectorAll('.pswp')[0];
        var items = [{
            src: s,
            w: img.width,
            h: img.height
        }];
        var options = {
            showAnimationDuration: 0,
            hideAnimationDuration: 0
        };
        var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);
        gallery.init();
        gallery.listen('destroy', function() {
            document.getElementById("check").style.display = "none";
            var elements = document.getElementsByTagName("input");
            for (var ii = 0; ii < elements.length; ii++) {
                if (elements[ii].type == "text") {
                    elements[ii].value = "";
                }
            }
            $$(window).width() < 770 && (myApp.allowPanelOpen = !0);
        });
    };
    openPhotoSwipe();
} 
img.onerror = function() {
    myApp.alert('Not found...');
    myApp.hidePreloader();
}}

1 个答案:

答案 0 :(得分:1)

试试这个:

function view(a) {
    var img = new Image();
    var s = a;
    myApp.showPreloader('?????????...');
    img.src = s;
    var alertShown = false;
    img.onload = function() {
        document.getElementById('showimg').innerHTML = '';
        var openPhotoSwipe = function(a) {
            myApp.hidePreloader();
            myApp.allowPanelOpen = false;
            document.getElementById("check").style.display = "block";

            var pswpElement = document.querySelectorAll('.pswp')[0];
            var items = [{
                src: s,
                w: img.width,
                h: img.height
            }];
            var options = {
                showAnimationDuration: 0,
                hideAnimationDuration: 0
            };
            var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);
            gallery.init();
            gallery.listen('destroy', function() {
                document.getElementById("check").style.display = "none";
                var elements = document.getElementsByTagName("input");
                for (var ii = 0; ii < elements.length; ii++) {
                    if (elements[ii].type == "text") {
                        elements[ii].value = "";
                    }
                }
                $$(window).width() < 770 && (myApp.allowPanelOpen = !0);
            });
        };
        openPhotoSwipe();
    } 
    img.onerror = function() {
        myApp.hidePreloader();
        if(!alertShown){
            alertShown = true;
            myApp.alert('Not found...');
        }
    }}
}