我想在javascript中创建一个全局计时器对象,然后能够动态添加回调.这样我就可以在我的脚本中使用一个全局计时器以一定的间隔执行所有操作,而不是通过使用倍数来浪费资源.
这就是我希望能够将事物拼凑在一起的方式:
var timer = new function() {
clearInterval( this.interval );
//[1] At this point I want the Callbacks to be run
var self = this;
setTimeout(function() {self.timer()}, 200);
}
function otherObject = new function() {
//When created I want to bind my object's function called cb to the global timer at [1]
}
otherObject.prototype.cb = function() {
//Stuff that should be done every time the timer is run
}
var someObject = new otherObject();
Run Code Online (Sandbox Code Playgroud)
我怎样才能将任何数字函数(大多数是其他对象中的函数)绑定到我的计时器间隔运行?
基本上我需要一个JS Regexp来弹出URL的最后一部分.它的关键是,虽然它只是域名,如http://google.com,我不希望任何改变.
以下是示例.任何帮助是极大的赞赏.
http://google.com -> http://google.com
http://google.com/ -> http://google.com
http://google.com/a -> http://google.com
http://google.com/a/ -> http://google.com/a
http://domain.com/subdir/ -> http://domain.com/subdir
http://domain.com/subfile.extension -> http://domain.com
http://domain.com/subfilewithnoextension -> http://domain.com
Run Code Online (Sandbox Code Playgroud)