Skip to content

Commit 7205c3a

Browse files
author
Jesse Martin
committed
clarify documentation
1 parent 279d23d commit 7205c3a

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@ var hello = new Notif({ text: 'Hello! This is a test message.' }); // shows the
1616
hello.close(); // removes the notification from the dom
1717
hello.show(); // renders and shows the notification again
1818
```
19+
20+
```js
21+
var helloAgain = new Notif({ text: 'A slightly more advanced message.' }).close({ duration: 3000 }); // renders and shows the notification, then waits 3000 ms and closes the message
22+
```

example/index.html

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@
77
</head>
88
<body>
99
<h1>Notif example</h1>
10-
<p>This page will show a notification after 2 seconds, then hide it 5 seconds later.</p>
1110
<script type="text/javascript">
12-
// show a notification 2 seconds after page load
13-
window.setTimeout(function () {
14-
var testNotif = new Notif({ text: 'This is a test message' });
11+
// show a hello notification
12+
var hello = new Notif({ text: 'Hello there!' });
1513

16-
// close it 5 seconds later
17-
window.setTimeout(function () {
18-
testNotif.close();
19-
}, 5000);
14+
// close it 5 seconds later
15+
window.setTimeout(function () {
16+
hello.close();
17+
}, 5000);
2018

19+
// say hello again 2 seconds after page load
20+
window.setTimeout(function () {
21+
// a more elegant way of closing a notification after a duration of time:
22+
var helloAgain = new Notif({ text: 'Hello, again.' }).close({ duration: 5000 });
2123
}, 2000);
2224
</script>
2325
</body>

notif.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ var Notif = (function () {
55
this.text = opts.text;
66
this.target = document.body;
77
this.show();
8+
return this;
89
}
10+
911
Notif.prototype.render = function () {
1012
var container = document.createElement('div');
1113
container.className = 'notif-container';
@@ -23,17 +25,20 @@ var Notif = (function () {
2325
container.appendChild(closeBtn);
2426
return container;
2527
};
26-
Notif.prototype.show = function (opts) {
28+
29+
Notif.prototype.show = function () {
2730
this.notifEl = this.render();
2831
this.target.appendChild(this.notifEl);
32+
return this;
33+
};
34+
35+
Notif.prototype.close = function (opts) {
2936
if (opts && opts.duration > 0) {
30-
window.setTimeout((function () {
31-
this.close();
32-
}).bind(this), opts.duration);
37+
window.setTimeout(this.close.bind(this), opts.duration);
38+
} else {
39+
this.target.removeChild(this.notifEl);
3340
}
34-
};
35-
Notif.prototype.close = function () {
36-
this.target.removeChild(this.notifEl);
41+
return this;
3742
};
3843

3944
return Notif;

0 commit comments

Comments
 (0)