小编Meh*_*hdi的帖子

获取语​​音列表语音合成Chrome(Web Speech API)

以下HTML在第一次单击时在控制台中显示空数组:

<!DOCTYPE html>
<html>
    <head>
        <script>
            function test(){
                console.log(window.speechSynthesis.getVoices())
            }
        </script>
    </head>
    <body>
        <a href="#" onclick="test()">Test</a>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

在第二次单击中,您将获得预期的列表.

如果添加onload事件来调用此函数(<body onload="test()">),则可以在第一次单击时获得正确的结果.请注意,第一次调用onload仍然无法正常工作.它在页面加载时返回空,但后续工作.

问题:

由于它可能是测试版中的错误,我放弃了"为什么"的问题.

现在,问题是您是否要window.speechSynthesis在页面加载时访问:

  • 这个问题最好的黑客是什么?
  • 如何speechSynthesis在页面加载时确保它会加载?

背景和测试:

我正在测试Web Speech API中的新功能,然后我在我的代码中遇到了这个问题:

<script type="text/javascript">
$(document).ready(function(){
    // Browser support messages. (You might need Chrome 33.0 Beta)
    if (!('speechSynthesis' in window)) {
      alert("You don't have speechSynthesis");
    }

    var voices = window.speechSynthesis.getVoices();
    console.log(voices) // []

    $("#test").on('click', function(){
        var voices = window.speechSynthesis.getVoices();
        console.log(voices); // …
Run Code Online (Sandbox Code Playgroud)

voice speech-synthesis dom-events webspeech-api

48
推荐指数
4
解决办法
2万
查看次数

如何在 python-socketio 中将消息从 python 服务器发送到 javascript 客户端?

socketio 客户端成功连接到服务器并向emit服务器发送消息,但向客户端的另一个方向服务器失败。我找不到错误的根源。这是

\n\n

这是基于python-socketioapp.py网站中的示例的服务器 python :

\n\n
from aiohttp import web\nimport socketio\n\nsio = socketio.AsyncServer()\napp = web.Application()\nsio.attach(app)\n\nasync def index(request):\n    """Serve the client-side application."""\n    with open(\'index.html\') as f:\n        return web.Response(text=f.read(), content_type=\'text/html\')\n\n@sio.on(\'connect\', namespace=\'/chat\')\ndef connect(sid, environ):\n    print("connect", sid)\n\n@sio.on(\'chat message\', namespace=\'/chat\')\nasync def message(sid, data):\n    print("server received message!", data)\n    await sio.emit(\'reply\', data)\n    await sio.send(data)\n\n@sio.on(\'disconnect\', namespace=\'/chat\')\ndef disconnect(sid):\n    print(\'disconnect\', sid)\n\napp.router.add_static(\'/static\', \'static\')\napp.router.add_get(\'/\', index)\n\nif __name__ == \'__main__\':\n    web.run_app(app)\n
Run Code Online (Sandbox Code Playgroud)\n\n

我尝试评论其中之一await sio.emit(\'reply\', data)await sio.send(data)但结果是相同的。这是 javascript 客户端index.html

\n\n
<html>\n  <head>\n    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.5/socket.io.min.js"></script>\n …
Run Code Online (Sandbox Code Playgroud)

javascript python websocket socket.io python-socketio

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

在JavaScript中过滤以字母开头的列表元素

我正在尝试将以'N'开头的列表元素整理到新列表中。为什么不起作用?

const countries = ['Norway', 'Sweden',  'Denmark', 'New Zealand'];

function firstN(){
  for (let i=0;i<countries.length;i++){
    countries[i].startsWith("N")
    }
}

let startsWithN = countries.filter(firstN())
Run Code Online (Sandbox Code Playgroud)

javascript arrays

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

计算特定字符串在给定单词中出现的次数

如何编写打印特定字符串出现在给定单词中的次数的程序.例如:如果我在单词'asdadgfrdad'中查找字符串'dad',则输出应为2.

def numStrings(a):
    strings = 'dad'
    result = 0
    for char in a:
        if char in strings:
            result = result + 1
    print result

numStrings("asdadgfrdad")
Run Code Online (Sandbox Code Playgroud)

但这给了我字母d,a出现在给定单词中的次数.怎么纠正这个?

python

2
推荐指数
1
解决办法
3654
查看次数