在 JavaScript 函数中插入旋转器。

huangapple go评论55阅读模式
英文:

Insert spinner on function javascript

问题

在JavaScript函数中插入旋转器。

大家好,我正在学习JavaScript的初级知识,我发现自己无法插入一个旋转器。我编写了以下代码,你能帮助我理解问题吗?

document.getElementById('insert').addEventListener('click',
    function(event) {
        event.preventDefault();
        var spinner = document.getElementById("spinner");
        spinner.style.display = "";
        insertDataToDatabase(results.data);
        spinner.style.display = "none";
    }, false);

function insertDataToDatabase(data) {
    data_noheader = data.slice(1);

    for (i = 0; i < data_noheader.length; i++) {
        $.ajax({
            url: 'index.php',
            type: 'POST',
            dataType: 'json',
            data: {
                action: 'import',
                data: data_noheader[i]
            },
            success: function(response) {

            },
            error: function(response) {

            },
        });
    }
}

我尝试在循环的第0个索引处激活旋转器,并在insertDataToDatabase函数完成后停用旋转器。

英文:

Insert spinner on function JavaScript.

Hi everyone, I'm taking my first steps with JavaScript and I find myself unable to insert a spinner
I wrote the following code, could you help me understand the problem?

document.getElementById(&#39;insert&#39;).addEventListener(&#39;click&#39;, 
    function(event) {
    event.preventDefault();
    var spinner = document.getElementById(&quot;spinner&quot;);														 
    spinner.style.display = &quot;&quot;;															 
    insertDataToDatabase(results.data);															 
    spinner.style.display = &quot;none&quot;;															
     }, false);

    function insertDataToDatabase(data) {
		
    data_noheader = data.slice(1);
		
 		for(i=0;i&lt;data_noheader.length;i++){
			$.ajax({
				url: &#39;index.php&#39;,
				type: &#39;POST&#39;,
				dataType: &#39;json&#39;,
				data: {
					action: &#39;import&#39;,
					data: data_noheader[i]
					},
					success: function(response) {

					},
					error: function(response) {
					},
			});
		}
	}`

I was trying to activate the spinner at index 0 of the loop and deactivate the spinner at the completion of the insertDataToDatabase function.

答案1

得分: 0

你应该使用Promisesasync/await来处理这个问题。

以下是示例:

document.getElementById("insert").addEventListener(
  "click",
  async function (event) {
    event.preventDefault();
    var spinner = document.getElementById("spinner");
    spinner.style.display = "";
    await insertDataToDatabase(results.data);
    spinner.style.display = "none";
  },
  false
);

async function insertDataToDatabase(data) {
  data_noheader = data.slice(1);

  for (i = 0; i < data_noheader.length; i++) {
    await $.ajax({
      url: "index.php",
      type: "POST",
      dataType: "json",
      data: {
        action: "import",
        data: data_noheader[i],
      },
      success: function (response) {},
      error: function (response) {},
    });
  }
}

或者更好的解决方案:

document.getElementById("insert").addEventListener(
  "click",
  async function (event) {
    event.preventDefault();
    var spinner = document.getElementById("spinner");
    spinner.style.display = "";
    await insertDataToDatabase(results.data);
    spinner.style.display = "none";
  },
  false
);

async function insertDataToDatabase(data) {
  data_noheader = data.slice(1);

  await Promise.all(data_noheader.map((row) => {
    return $.ajax({
      url: "index.php",
      type: "POST",
      dataType: "json",
      data: {
        action: "import",
        data: row,
      },
      success: function (response) {},
      error: function (response) {},
    });
  }));
}
英文:

You should handle this with Promises or async/await.

Here are the example:

document.getElementById(&quot;insert&quot;).addEventListener(
  &quot;click&quot;,
  async function (event) {
    event.preventDefault();
    var spinner = document.getElementById(&quot;spinner&quot;);
    spinner.style.display = &quot;&quot;;
    await insertDataToDatabase(results.data);
    spinner.style.display = &quot;none&quot;;
  },
  false
);

async function insertDataToDatabase(data) {
  data_noheader = data.slice(1);

  for (i = 0; i &lt; data_noheader.length; i++) {
    await $.ajax({
      url: &quot;index.php&quot;,
      type: &quot;POST&quot;,
      dataType: &quot;json&quot;,
      data: {
        action: &quot;import&quot;,
        data: data_noheader[i],
      },
      success: function (response) {},
      error: function (response) {},
    });
  }
}

Or even a bit better solution:

document.getElementById(&quot;insert&quot;).addEventListener(
  &quot;click&quot;,
  async function (event) {
    event.preventDefault();
    var spinner = document.getElementById(&quot;spinner&quot;);
    spinner.style.display = &quot;&quot;;
    await insertDataToDatabase(results.data);
    spinner.style.display = &quot;none&quot;;
  },
  false
);

async function insertDataToDatabase(data) {
  data_noheader = data.slice(1);

  await Promise.all(data_noheader.map((row) =&gt; {
    return $.ajax({
      url: &quot;index.php&quot;,
      type: &quot;POST&quot;,
      dataType: &quot;json&quot;,
      data: {
        action: &quot;import&quot;,
        data: row,
      },
      success: function (response) {},
      error: function (response) {},
    });
  }));
}

huangapple
  • 本文由 发表于 2023年5月22日 18:44:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305384.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定