-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdemo.html
45 lines (39 loc) · 1.23 KB
/
demo.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<html>
<head>
<title>Demo of Timely in Browser</title>
<head>
<body>
<div id="result"></div>
<script src="lib/timely.js"></script>
<script>
(function() {
// Synchronous function (Fibonacci)
var fib = function(n) {
if (n <= 2) return n;
return fib(n - 1) + fib(n - 2);
},
// Asynchronous function
wait = function(n, cb) {
setTimeout(function() {
cb(n);
}, 100);
},
// Create a timed verion of the Synchronous funciton
fibT = timely(fib),
// Create a timed verion of the Asynchronous function
waitT = timely.async(wait),
// Result of Synchronous function
resultSync;
// Call Synchronous function
resultSync = fibT(35);
//Output results of Synchronous function. fibT.time contains the time used for the last call
document.getElementById('result').innerHTML += '<p>fib(35) = ' + resultSync + ', time: ' + fibT.time + 'ms</p>';
// Call Asynchronous function
waitT(42, function(resultAsync) {
//Output results of Asynchronous function. fibT.time contains the time used for the last call
document.getElementById('result').innerHTML += '<p>wait(42) = ' + resultAsync + ', time: ' + waitT.time + 'ms</p>';
});
})();
</script>
</body>
</html>