标签: asynchronous-javascript

将脚本放在底部的两种不同方式 - 有什么区别?

以下两种解决方案有何区别?特别是,有一个很好的理由支持2超过1.(注意:请假设要加载的脚本的名称是已知的.问题是关于创建最小脚本以在给定的脚本中加载脚本是否有价值情况)

1 - 底部的脚本

<html>
<body>
...
...
<script src='myScript.js'></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

2 - 底部的脚本加载外部脚本

<html>
<body>
...
...
<script>
    // minimal script to load another script
    var script = document.createElement('script');
    script.src = 'myScript.js'
    document.body.appendChild(script);
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

html javascript asynchronous-javascript

18
推荐指数
2
解决办法
663
查看次数

为什么使用“URL.createObjectURL(blob)”而不是“image.src”?

Q1. 在异步 JavaScript 的上下文中,并且需要从客户端获取 xe2x80x98 数据,为什么我们不能只通过图像元素的属性来编辑图像元素呢src

\n\n

Q2。为什么需要经历Blob转换过程?

\n\n

Q3。blob 的作用是什么?

\n\n

例如从 JSON 检索图像文件。(顺便说一句,我从 MDN 网页上提取的,请注意评论)

\n\n
\n  function fetchBlob(product) {\n    // construct the URL path to the image file from the product.image property\n    let url = 'images/' + product.image;\n    // Use fetch to fetch the image, and convert the resulting response to a blob\n    // Again, if any errors occur we report them in the console.\n    fetch(url).then(function(response) {\n        return response.blob();\n    }).then(function(blob) {\n      // Convert the …
Run Code Online (Sandbox Code Playgroud)

javascript json blob asynchronous-javascript

16
推荐指数
1
解决办法
5万
查看次数

Nodejs + Mongodb:聚合后查找数据

我是 Nodejs 和 MongoDB 的新手。
这是我的数据集示例:

{ 
  'name': ABC,
  'age':24,
  'gender':male,
  ...
}
Run Code Online (Sandbox Code Playgroud)

一般来说,我想做的是先聚合数据,然后再使用它们来查找不同的数据簇。
具体来说,我想知道不同年龄的人有多少。然后,找到每个年龄的人(文件)并存储它们。

这是我的代码:

MongoClient.connect(url, function(err, db) {
    if(err) { 
        console.log('Unable to connect to the mongoDB server. Error:', err); 
    } else { 
        db.collection('test').aggregate(
        [
          { $group: { _id: "$age" , total: { $sum: 1 } } },
          { $sort: { total: -1 } } 
        ]).toArray(function(err, result) {
            assert.equal(err, null);
            age = [];
            for(var i in result) {
                age.push(result[i]['_id'])
            };
            ageNodes = {};
            for(var i in age) {
                 nodes …
Run Code Online (Sandbox Code Playgroud)

mongodb node.js mongodb-query aggregation-framework asynchronous-javascript

4
推荐指数
1
解决办法
7077
查看次数

异步函数作为 prop 传递到 React 组件中,导致 @typescript-eslint/no-misused-promises 错误

I have the following asynchronous submitNewPatient function which is throwing @typescript-eslint/no-misused-promises error message from elint. Is it possible to adjust the function such that it removes this error?

const submitNewPatient = async (values: PatientFormValues) => {
    try {
      const { data: newPatient } = await axios.post<Patient>(
        `${apiBaseUrl}/patients`,
        values
      );
      dispatch({ type: "ADD_PATIENT", payload: newPatient });
      closeModal();
    } catch (e: unknown) {
      if (axios.isAxiosError(e)) {
        console.error(e?.response?.data || "Unrecognized axios error");
        setError(
          String(e?.response?.data?.error) || "Unrecognized axios error"
        );
      } else {
        console.error("Unknown …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs asynchronous-javascript axios react-functional-component

4
推荐指数
1
解决办法
4396
查看次数

延迟加载 JavaScript - Uncaught ReferenceError: $ is not defined

我使用谷歌代码来推迟加载 javascript(谷歌页面

但我有一些内联 javascript,例如:

<script type="text/javascript">
$(function () {
    alert("please work");
});
</script>
Run Code Online (Sandbox Code Playgroud)

这给了我:

未捕获的 ReferenceError: $ 未定义

我想我需要一些函数,它是在加载 jQuery 并初始化我的内联 javascripts 之后触发的。但如果有另一种方式,我会很高兴。

编辑:

你们中的一些人完全没有话题。Mahesh Sapkal 很接近。

使用此代码我没有错误,但仍然无法正常工作

<head>
    <script type="text/javascript">
        var MhInit = NULL;

        // Add a script element as a child of the body
        function downloadJSAtOnload() {
            var element = document.createElement("script");
            MhInit = element.src = "my_packed_scripts.js";
            document.body.appendChild(element);
        }

        // Check for browser support of event handling capability
         if (window.addEventListener)
            window.addEventListener("load", downloadJSAtOnload, false);
         else if (window.attachEvent)
             window.attachEvent("onload", …
Run Code Online (Sandbox Code Playgroud)

javascript jquery deferred-loading asynchronous-javascript

3
推荐指数
1
解决办法
1万
查看次数

React hooks:如何检测特定状态变量何时更新

在 React hooks 之前,我会使用componentDidUpdate(prevProps, prevState),如果我想仅在更新时执行给定的代码this.state.a,我会这样做

if (prevState.a !== this.state.a) {
  <...>
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能实现同样的事情useEffect()

javascript reactjs react-native asynchronous-javascript react-hooks

3
推荐指数
1
解决办法
1万
查看次数