0

我有一个单击事件,它在数据库表上执行查询,并且应该根据查询返回的对象的长度触发不同的警报。该查询检查表以查看用户是否已经存储了与特定对象 ID 相关的用户信息。id 代表一个事件。我想防止用户为同一事件存储他们的信息两次。因此,当第一次单击链接时,查询会检查表以查看他们的信息是否已经与该事件 ID 相关,如果没有,则将其信息与事件 ID 一起存储;但是当第二次点击该链接时,我想提醒一条消息,告诉用户他们已经提交了他们的信息。由于某种原因,当我测试它时,第二次条件不起作用,

$("#mainDiv").on('click', '.interested', function (event) {
    //get id attribute of the clicked "interested" link. It stores the id of the activity.
    var activityID = $(event.target).attr('id');

    /*instantiate object that will hold info about interested user. The constructor function and variables are declared in a different part of my code*/
    var intrstd = new interested(hood, gender, dateOfBirth, zip, uID);

    /*check the "Interested" table on Parse to see if the user already has info stored for this activity */
    var check = Parse.Object.extend("Interested");
    var interest = new Parse.Query(check);

    //these are query constrains that pick out the colum values I want. 
    interest.equalTo("parent", activityID);
    interest.equalTo("InterestedPPL", intrstd.uID);

    //interest.find() executes the query and "results" contains the return object
    interest.find({
        success: function (results) {
            /*if the user info is already saved for the specific id, show the alert, if not store the object containing their user info */
            if (results.length !== 0) {
                alert("you have already requested an invite to that activity");
            } else {
                var show = Parse.Object.extend("Interested");
                var int = new show();
                int.set("parent", activityID);
                int.set("InterestedPPL", intrstd);
                int.save();
                alert("you will be notified if you have been invited to this activity");
            } //closes else   
        },
        error: function (object, error) {
            // The object was not retrieved successfully.
            // error is a Parse.Error with an error code and description.
            console.log("Error: " + error.code + " " + error.message);
        }
    }); //closes find
}); //closes on
4

1 回答 1

1

_clickFlag在事件处理程序之外声明一个变量。_clickFlag如果为true则处理事件。在运行查询之前,将其设置为false以便不处理连续点击。在您的查询成功回调中,再次设置_clickFlagtrue。这种方法有一个优点。它根本不会进行额外的查询。希望我足够清楚。请参阅下面的代码示例。

var _clickFlag = true;
$("#mainDiv").on('click', '.interested', function(event){

    if (_clickFlag) {// Continue only if true

        _clickFlag = false; // this is to prevent other consecutive clicks to be processed when
        .
        .
        .

        interest.find({ success: function(results) {

            var show = Parse.Object.extend("Interested");
            var int = new show();
            int.set("parent", activityID);
            int.set("InterestedPPL", intrstd);
            int.save();

            _clickFlag = true;

            alert("you will be notified if you have been invited to this activity");
    .
    .
    .
    .
于 2013-07-02T02:33:40.423 回答