简体   繁体   English

如何防止 promise 在页面加载时被调用

[英]how to prevent promise getting called on page load

I am new to the concept of promise in JavaScript.我是 JavaScript 中 promise 概念的新手。 I have tried to implement promise in my but don't know why it is getting on page load.我试图在我的中实现 promise 但不知道为什么它正在加载页面。 I know this not a tutorial website but any help would help me to improve my concept of promise.我知道这不是一个教程网站,但任何帮助都会帮助我改进我对 promise 的概念。 I tried to google the solution for my problem but I was not able to find the solution.我试图用谷歌搜索我的问题的解决方案,但我找不到解决方案。 As per my logic SearchRecord function should be called on button click but it is getting called on page load根据我的逻辑 SearchRecord function 应该在按钮单击时调用,但在页面加载时会被调用

 <button id="Query" class="btn btn-warning" onclick="SearchRecord()" value="Search"><i class="fas fa-search"></i> Search</button>
function SearchRecord() {         
    let InvOrg = $('#InvOrg').val();
    let ItemCode = $('#ItemCode').val();
    let Description = $('#Description').val();

     return new Promise((resolve, reject) => {
        $.ajax({
            url: '/GINV/GINV01/tableData',
            type: 'GET',
            async:'true',
            data: {
                InvOrg: InvOrg, ItemCode: ItemCode, Description: Description
            },
            success: function (data) {
                resolve(data)
            },
            error: function (error) {
                console.log(error)
                reject(error)
            },
        })
     })
              
}
SearchRecord()
    .then((data) => {
        console.log(data)
        tabledata(data)
    })
    .catch((error) => {
        console.log(error)
    })
function tabledata(data) {
    debugger;
    if ($.fn.DataTable.isDataTable('#MainTable')) {
        $('#MainTable').DataTable().destroy();
    }
    $('#MainTable tbody').empty();

    var table = $('#MainTable').DataTable({
        data: data.listIndexData,
        stateSave: true,
        "columns": [
          
            { "data": "itemCode"  },

            { "data": "itemDescription" },

            { "data": "uom" },

            { "data": "quantity", "className": "text-right" },

            { "data": "pslRate", "className": "text-right"  },

            { "data": "value", "className": "text-right" },

            { "data": "unit" },

        ],
    });
   

}

It's because you're invoking the SearchRecord function.这是因为您正在调用 SearchRecord function。

SearchRecord()
    .then((data) => {
        console.log(data)
        tabledata(data)
    })
    .catch((error) => {
        console.log(error)
    })

The above code should be contained in another function.上面的代码应该包含在另一个 function 中。

 <button id="Query" class="btn btn-warning" onclick="anotherFunction()" value="Search"><i class="fas fa-search"></i> Search</button>

function anotherFunction(){
  SearchRecord()
    .then((data) => {
        console.log(data)
        tabledata(data)
    })
    .catch((error) => {
        console.log(error)
    })
}

I was able to find the answer to this question with the help of Bergi although Abdulla's answer is also right.尽管 Abdulla 的回答也是正确的,但我在 Bergi 的帮助下找到了这个问题的答案。 I wanted to this without making a new function.我想在不制作新 function 的情况下做到这一点。

so per the suggestion of Bergi Answer to this question is所以根据 Bergi 的建议,这个问题的答案是

 <button id="Query" class="btn btn-warning" onclick="SearchRecord().then(tabledata).catch((error) => { console.log(error) })" value="Search"><i class="fas fa-search"></i> Search</button>
               

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

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