简体   繁体   English

为什么phonegap点击事件会引发两次?

[英]Why phonegap click event fires twice?

I am developing android app using phonegap 1.5.0 我正在使用phonegap 1.5.0开发Android应用程序

App stores data in local database and synchronizes using web services. App将数据存储在本地数据库中,并使用Web服务进行同步。 I am using one button click to call synchronize data JavaScript function. 我使用一键单击调用同步数据JavaScript函数。 What I have found is: 我发现的是:

This synch button's on click event fires twice. 单击事件上的此同步按钮会触发两次。 Single record is getting uploaded twice and the uploaded time is same in the server database. 单个记录上传两次,上传时间在服务器数据库中相同。

How to avoid this? 怎么避免这个?

Thanks 谢谢

EDIT: 编辑:

Hey everyone, sorry for the delay in reply; 大家好,抱歉延迟回复; was bit busy with some priority work. 有点忙于一些优先工作。

Calling SyncData() function on click of button. 单击按钮调用SyncData()函数。

Here is the code: 这是代码:

document.addEventListener("DOMContentLoaded", onDeviceReady, false);
var db;
var maxrecords = 0;
var recordsprocessed = 0;
var urlstart = "http://www.mywebsite.com/";

function onDeviceReady() {
    db = window.openDatabase("LocalDB", "1.0", "PhoneGap Demo", 50 * 1024 * 1024);
}

function SyncData() {
    maxrecords = 0;
    recordsprocessed = 0;
    db.transaction(queryDB, errorCB, successCB);
}

function queryDB(tx) {
    var entriestoupload = 0;
    var profimagetoupload = 0;

    //check how many entries are to be synchronized
    tx.executeSql('SELECT count(*) as cnt FROM tblEntries WHERE IsUploaded=0', [], function(tx, results) {
        entriestoupload = parseInt(results.rows.item(0)['cnt']);
        //check how many profile images are to be uploaded
        tx.executeSql('SELECT count(*) as cnt FROM tblEntries WHERE IsProfileImageUploaded=0', [], function(tx, results) {
            profimagetoupload = parseInt(results.rows.item(0)['cnt']);

            //synch proceeds if there is any record which is not sychronised
            if (entriestoupload > 0 || profimagetoupload > 0) {
                var dataMsg = '';
                var porofimgMsg = '';
                if (entriestoupload > 0) dataMsg = entriestoupload + ' entry, ';
                if (profimagetoupload > 0) porofimgMsg = profimagetoupload + ' profile image ';

                // give user exact info about what will be synchronized 
                if (confirm(dataMsg + porofimgMsg + ' are not synchronized.\n Do you want to start synchronisation? \n Please wait till synch successfull message appears.')) {

                    //start synchronisation
                    tx.executeSql('SELECT * FROM tblEntries ORDER BY DateOfRegistration DESC', [], uploadData, errorCB);
                }
            } else {
                alert('All records are already Synchronized.');
            }
        }, errorCB);
    }, errorCB);
}

function uploadData(tx, results) {
    var len = results.rows.length;
    maxrecords = len;
    var Synched = 0;
    if (len > 0) {
        for (var i = 0; i < len; i++) {
            var row = results.rows.item(i);
            var LocalId = row['LocalId'];
            var DateOfRegistration = getDateTimeformatMySql(String(row['DateOfRegistration']));
            var DateOption = getDateTimeformatMySql(String(row['DateOption']));
            var VolunteerId = row['VolunteerId'];
            var IsUploaded = row['IsUploaded'];
            var LiveId = row['LiveId'];
            var ProfileImagePath = row['ProfileImagePath'];
            var IsProfileImageUploaded = parseInt(row['IsProfileImageUploaded']);
            var params = null;
            var weburl = null;

            if (IsUploaded == 0) {

                //set parameters for web service
                params = "LocalId=" + LocalId + "&OrganizationName=" + row['OrganizationName'] + "&FirstName=" + row['FirstName'] + "&LastName=" + row['LastName'] + "&EmailAddress=" + row['EmailAddress'] + "&MobileNumber=" + row['MobileNumber'] + "&Country=" + row['Country'] + "&State=" + row['State'] + "&City=" + row['City'] + "&Lattitude=" + row['Lattitude'] + "&Longitude=" + row['Longitude'] + "&Website=" + row['Website'] + "&DateOption=" + DateOption + "&TimeOption=" + row['TimeOption'] + "&NumberOption=" + row['NumberOption'] + +"&RadioOption=" + row['RadioOption'] + "&Details=" + row['Details'] + "&CheckBoxoption=" + row['CheckBoxoption'] + "&DropDownOption=" + row['DropDownOption'] + "&DateOfRegistration=" + DateOfRegistration + "&VolunteerId=" + VolunteerId;

                //web service url
                weburl = urlstart + "mywebserviceurl";

                try {
                    $.ajax({
                        async: false,
                        type: "POST",
                        url: weburl,
                        data: params,
                        dataType: "json",
                        success: function(data, textStatus, jqXHR) {
                            if (data.Success == "0") {
                                alert('web services error:\n' + data.Message);
                            }
                            if (data.Success == "1") {
                                try {
                                    LiveId = parseInt(String(data.LiveId));
                                    IsUploaded = 1;

                                    //Update local database to set IsUploaded and LiveId
                                    tx.executeSql("UPDATE tblEntries SET LiveId= " + LiveId + " ,IsUploaded=1 WHERE LocalId= " + LocalId, [], function(tx, results) {
                                        Synched = Synched + 1; /*alert(LiveId);*/
                                    }, errorCB);
                                    //check if profile image exists or not
                                    if (ProfileImagePath != undefined) {
                                        uploadImage(LiveId, LocalId, ProfileImagePath, '1', '');
                                    }

                                } catch (e) {
                                }
                            }
                        },
                        error: function() {
                            alert("There was an error loading the feed");
                        }
                    });
                } catch (e) {
                }
            } else {
                //check if data is uploaded and image is not uploaded
                if (IsProfileImageUploaded == 0 && ProfileImagePath != undefined) {
                    uploadImage(LiveId, ShopId, ProfileImagePath, '1', '');
                }
            }
        }
    }
} // end of querySucess function

//function to upload image

function uploadImage(LiveId, LocalId, ImagePath, UploadType, CreationDate) {
    try {

        var options = new FileUploadOptions();
        options.fileKey = "file";
        options.fileName = ImagePath;
        options.mimeType = "image/jpg";

        var params = new Object();
        params.LiveId = LiveId;
        params.LocalId = LocalId;
        params.UploadType = UploadType;
        params.CreationDate = CreationDate;
        options.params = params;
        options.chunkedMode = false;
        var ft = new FileTransfer();
        var url = urlstart + "mywebservice_url_to_uploadimage";

        ft.upload(ImagePath, url, win, fail, options, false);

    } catch (e) {
        console.error("Survey App Err :" + e.message);
    }
}

function win(r) {
    var jsonresponse = r.response.substring(r.response.indexOf('{'), r.response.indexOf('}') + 1);
    var obj = $.parseJSON(jsonresponse);

    if (obj.Success == "1") {

        var LocalId = parseInt(obj.LocalId);
        var UploadType = parseInt(obj.UploadType);
        if (UploadType == 1) {
            db.transaction(function(tx) {
                tx.executeSql("UPDATE tblEntries SET IsProfileImageUploaded=1 WHERE LocalId=?", [LocalId], function(tx, results) {
                }, errorCB);
            }, errorCB, successCB);
        }
    }
}

function fail(error) {
    alert("There was an error uploading image");
}

我找到的唯一解决方案是使用tap而不是click,它将解决问题。

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

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