js-sys offers bindings to all the global APIs available in every JavaScript environment as defined by the ECMAScript standard.

In this lesson, we will install and use js-sys to invoke JavaScript's Date API to grab the current time. The date will need to be converted to a value Rust can use and display that date from Rust in the browser.

Cargo.toml:

[dependencies]
cfg-if = "0.1.5"
wasm-bindgen = "0.2.25"
js-sys = "0.2"

lib.rs:

// Called by our JS entry point to run the example
#[wasm_bindgen]
pub fn run() {
let now = js_sys::Date::now();
let now_date = js_sys::Date::new(&JsValue::from_f64(now)); let val = document.createElement("p"); val.set_inner_html(&format!(
"Hello from Rust, it's {}:{}",
now_date.get_hours(),
now_date.get_minutes()
));
document.body().append_child(val);
}

index.js:

import("../crate/pkg").then(module => {
module.run();
});

[WASM Rust] Use the js-sys Crate to Invoke Global APIs Available in Any JavaScript Environment的更多相关文章

  1. Day.js 是一个轻量的处理时间和日期的 JavaScript 库

    Day.js 是一个轻量的处理时间和日期的 JavaScript 库,和 Moment.js 的 API 设计保持完全一样. 如果您曾经用过 Moment.js, 那么您已经知道如何使用 Day.js ...

  2. [WASM Rust] Create and Publish a NPM Package Containing Rust Generated WebAssembly using wasm-pack

    wasm-pack is a tool that seeks to be a one-stop shop for building and working with Rust generated We ...

  3. [WASM + Rust] Debug a WebAssembly Module Written in Rust using console.log

    Having some kind of debugging tool in our belt is extremely useful before writing a lot of code. In ...

  4. Ext JS学习第九天 Ext基础之 扩展原生的javascript对象

    此文来记录学习笔记: •Ext对于原生的javascript对象进行了一系列的扩展,我们把他们掌握好,更能深刻的体会Ext的架构,从而对我们的web开发更好的服务, 源码位置,我们可以从开发包的这个位 ...

  5. js 计算当年还剩多少时间的倒数计时 javascript 原理解析【复制到编辑器查看推荐】

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. JS的六大对象:Global、Math、Number、Date、JSON、console,运行在服务器上方的支持情况分析

    在ASP中使用runat="server"来调用JS的相关函数,代码如下: <script runat="server" language="j ...

  7. face-api.js:一个在浏览器中进行人脸识别的 JavaScript 接口

    Mark! 本文将为大家介绍一个建立在「tensorflow.js」内核上的 javascript API——「face-api.js」,它实现了三种卷积神经网络架构,用于完成人脸检测.识别和特征点检 ...

  8. js中获取一个对象里面的方法和属性的javascript

    <script type="text/javascript"> var obj = { attribute:1, method:function() { alert(& ...

  9. 为什么很多国内公司不使用 jQuery 等开源 JS 框架(库),而选择自己开发 JavaScript 框架?

    http://www.zhihu.com/question/20099586/answer/13971670 我对公司JAVASCRIPT框架的定位思考:

随机推荐

  1. 验证debug思路之从寄存器开始

    对于boot a peripherial or module 一般都是配置一系列的寄存器(有可能有时间等方便的写入读出要求). 1.确保寄存器的读写按spec要求完成.<====可以通过波形查看 ...

  2. docker apache安装

    文章来源: 1.添加镜像 # docker pull httpd2. 2.创建httpd文件 # mkdir /data/httpd2. 3.启动apache # docker run -it -p ...

  3. 【转】ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

    为了加强安全性,MySQL5.7为root用户随机生成了一个密码,在error log中,关于error log的位置,如果安装的是RPM包,则默认是/var/log/mysqld.log. 一般可通 ...

  4. UIBarButtonSystemItem 样式

    使用时需要注意创建方式的区别: 01 typedef enum { 02     UIBarButtonSystemItemDone, 03     UIBarButtonSystemItemCanc ...

  5. PAT Basic 1042

    1042 字符统计 请编写程序,找出一段给定文字中出现最频繁的那个英文字母. 输入格式: 输入在一行中给出一个长度不超过 1000 的字符串.字符串由 ASCII 码表中任意可见字符及空格组成,至少包 ...

  6. 笔记-python-字符串格式化-format()

    笔记-python-字符串格式化-format() 1.      简介 本文介绍了python 字符串格式化方法format()的常规使用方式. 2.      使用 2.1.    Accessi ...

  7. Python 前端 Html基础

    概述 HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页的标准语言.相当于定义统一 的规则.大家都来遵守它,这样就可以让浏览器根据标记语 ...

  8. 二丶人生苦短,我用python【第二篇】

    1 编码 python解释器在加载 .py 文件中的代码时,对内容默认进行ascill编码,因此存在中文会报错,所以需要告诉python解释器,用什么编码来执行源代码.) ASCII码,主要用于显示现 ...

  9. 学习笔记7——wp版本更新需要注意的问题

    平时开发时应该避免修改wp的核心代码, 因为在升级wp版本时,核心代码都会被覆盖, wp升级时只有wp-content文件夹不会被覆盖.

  10. Wordpress无法上传图片

    当在自己新搭建的个人网站添加文章的时候,我遇到了wordpress最普遍的第一个问题——无法上传图片.     每次图片上传完成之后跳出如上图无法建立目录的警告,根据提示首先确定是否有修改文件的权限通 ...