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. python爬虫基础02-urllib库

    Python网络请求urllib和urllib3详解 urllib是Python中请求url连接的官方标准库,在Python2中主要为urllib和urllib2,在Python3中整合成了urlli ...

  2. UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 287: ordinal not in range(128)

    python的str默认是ascii编码,和unicode编码冲突,就会报这个错误. import sys reload(sys) sys.setdefaultencoding('utf8')

  3. shell-code-3-echo用法&printf用法

    ××××××××××××××××××××××××××××××下面是echo××××××××××××××××××××××××××××××× # read 命令从标准输入(即执行时,键盘的输入)中读取一行 ...

  4. ZOJ 2058 The Archaeologist's Trouble II(贪心+模拟)

    [题目大意] 一个n高的塔,由@ * ?三种字符组成.每行相邻两个字符不能相邻. '?' 表示未确定是 '@' 还是 '*' . 求'@' 可能出现的最多和最少次数. [分析] 在可以填的情况下 先填 ...

  5. 爬虫cookie

    Cookie Cookie 是指某些网站服务器为了辨别用户身份和进行Session跟踪,而储存在用户浏览器上的文本文件,Cookie可以保持登录信息到用户下次与服务器的会话. Cookie原理 HTT ...

  6. HDU 3516 DP 四边形不等式优化 Tree Construction

    设d(i, j)为连通第i个点到第j个点的树的最小长度,则有状态转移方程: d(i, j) = min{ d(i, k) + d(k + 1, j) + p[k].y - p[j].y + p[k+1 ...

  7. Python学习案例

    例1.求101到200之间所有的质数,并打印总数. 说明:除去1和它本身之外,不能被其他数整除,就是质数. #!/bin/python #-*- coding:utf-8 -*- #使用集合法 l = ...

  8. Oc_总结

    1.定义类: @interface 类名 : 父类 @end 2.使用:(冒号)表示继承一个类 Student : NSObject 3.使用()定义一个Catagory(类别) * 作用:在不改变原 ...

  9. zoj 1760 Doubles

    Doubles Time Limit: 2 Seconds      Memory Limit: 65536 KB As part of an arithmetic competency progra ...

  10. 也来“玩”Metro UI之磁贴(一)

    Win8出来已有一段时间了,个人是比较喜欢Metro UI的.一直以来想用Metro UI来做个自己的博客,只是都没有空闲~~今天心血来潮,突然想自己弄一个磁贴玩玩,动手……然后就有了本篇. Win8 ...