通过http启动/停止和处理程序自定义调度程序(start/stop and handler custom scheduler via http)
我想创建一个调度程序,以便它每秒执行一个任务,但也希望有一个和http接口来停止/启动调度程序并获得更多的统计信息/信息,在阅读更多关于计时器和代码 , 通道和gorutines我出来了:
https://gist.github.com/nbari/483c5b382c795bf290b5
package main import ( "fmt" "log" "net/http" "time" ) var timer *time.Ticker func scheduler(seconds time.Duration) *time.Ticker { ticker := time.NewTicker(seconds * time.Second) go func() { for t := range ticker.C { // do stuff fmt.Println(t) } }() return ticker } func Start(timer *time.Ticker) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { timer = scheduler(1) w.Write([]byte("Starting scheduler")) }) } func Stop(timer *time.Ticker) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { timer.Stop() w.Write([]byte("Stoping scheduler")) }) } func main() { timer = scheduler(1) http.Handle("/start", Start(timer)) http.Handle("/stop", Stop(timer)) log.Fatal(http.ListenAndServe(":8080", nil)) }
上面的代码工作,但我有一个全局“计时器”变量,我想知道是否有更好的方法来实现这个,也是一种方法来处理超过1个调度程序,目前正在考虑可能实现一个容器的类型所有的调度程序,但希望有一些反馈,可以帮助我找到聪明的解决方案。
I want to create a scheduler so that it executes a task every second for example, but also would like to have and http interface to stop/start the scheduler and get more stats/info, after reading more about timers & tickers, channels and gorutines I came out with this:
https://gist.github.com/nbari/483c5b382c795bf290b5
package main import ( "fmt" "log" "net/http" "time" ) var timer *time.Ticker func scheduler(seconds time.Duration) *time.Ticker { ticker := time.NewTicker(seconds * time.Second) go func() { for t := range ticker.C { // do stuff fmt.Println(t) } }() return ticker } func Start(timer *time.Ticker) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { timer = scheduler(1) w.Write([]byte("Starting scheduler")) }) } func Stop(timer *time.Ticker) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { timer.Stop() w.Write([]byte("Stoping scheduler")) }) } func main() { timer = scheduler(1) http.Handle("/start", Start(timer)) http.Handle("/stop", Stop(timer)) log.Fatal(http.ListenAndServe(":8080", nil)) }
The above code is working but I have a global "timer" variable, I would like to know if there is a better way to implement this and also a way for handle more than 1 scheduler, currently thinking on probably implementing kind of a container for all the scheduler but would like to have some feedbacks that could help me find clever solutions.
原文:https://stackoverflow.com/questions/33413105
最满意答案
只需在
.hover()
添加另一个动画(它有两个参数:鼠标输入和鼠标离开)Just add another animation in the
.hover()
(which takes two parameters : mouse enter and mouse leave)
相关问答
更多-
替换以下代码: $(".chart.electric li, .chart.fuel li").each(function (index) 对于这一个: $( $(".chart.electric li, .chart.fuel li").get().reverse() ).each(function (index) { 看工作演示 Replace your following code: $(".chart.electric li, .chart.fuel li").each(function (in ...
-
只需更改你的jquery就可以使用.toggleClass()完成你的整个事情: $('.linguait').click(function() { $(this).toggleClass("active"); }); 并在linguait类中设置标准(停用)而不是自己的标准。 linguait.active { right:25%; margin-right: -10px; -webkit-transform: rotate(360deg); transform: ...
-
SVG元素的反向动画(Reverse animation of SVG elements)[2021-09-20]
由于您正在寻找仅具有两种不同状态的切换效果,因此使用转场而不是动画会更好。 删除类时,转换可以自动产生相反的效果。 转换演示: window.onload = function() { var btn = document.querySelector('button'); btn.addEventListener('click', function() { document.querySelector('.container').classList.toggle('opened'); ... -
我不知道这是否是最有效的方法,但添加反向关键帧和切换类似乎可行。 codepen示例 #path1.active { animation: line 1.5s forwards; } #path1.inactive { stroke-dashoffset: -15.5; animation: unline 1s linear forwards; } #halfLineLeft.active { animation: shrinkleft 1s linear forwards; } #h ...
-
反向jquery动画(Reverse jquery animation)[2023-04-04]
改变这一行的marginTop:30 $(this).stop().animate({opacity:1, marginTop:30},'slow'); 至 $(this).stop().animate({opacity:1, marginTop:-30},'slow'); Change this line's marginTop:30 $(this).stop().animate({opacity:1, marginTop:30},'slow'); to $(this).stop().animate ... -
如何将jQuery动画反转回原始CSS属性(How to reverse a jQuery animation back to original CSS properties)[2022-04-04]
你可以用普通的CSS做转换。 .animate-me { background-color:red; width:50px; height:50px; position:absolute; top:0; left:150px; transition:left 2s, background-color 2s; } .animate-me:hover{ left:0; background-color:blue; ... -
只需在.hover()添加另一个动画(它有两个参数:鼠标输入和鼠标离开) http://jsfiddle.net/aGcpR/12/ Just add another animation in the .hover() (which takes two parameters : mouse enter and mouse leave) http://jsfiddle.net/aGcpR/12/
-
如何使绿色摇滚动画反向但比原始动画更快?(How to make a greensock animation reverse but faster than original animation?)[2022-03-11]
它应该像将timeScale()设置为3一样简单(意味着它的播放速度比正常速度快3倍): tl.timeScale(3); 请记住,你可以链接东西,所以你可以同时反转()它: tl.reverse().timeScale(3); 然后当你需要再次前进时: tl.play().timeScale(1); 这些都在文档中: http : //greensock.com/docs/#/HTML5/GSAP/ 我还注意到你在3秒后使用setTimeout()来调用contact()。 这很好,但是如果你想在时 ... -
反向jQuery高度动画(Reverse jQuery Height Animation)[2022-02-16]
看看这个: http : //codepen.io/anon/pen/EmauB 我对叠加和内容使用了不同的布局。 例如,使用4个图像。 HTML代码jQuery Reverse Animation
-
Image 1
- ...
-
-
$('#showmenu1').click(function() { $('.menu1').slideToggle("slow"); }); 演示: http : //jsfiddle.net/wL71w8td/ $('#showmenu1').click(function() { $('.menu1').slideToggle("slow"); }); Demo : http://jsfiddle.net/wL71w8td/