只是想问一下如何创建最简单的倒数计时器.
网站上会有一句话说:
"注册于05:00关闭!"
所以,我想要做的是创建一个简单的js倒计时器,从"05:00"到"00:00",然后一旦结束就重置为"05:00".
我以前经历过一些答案,但是对于我想做的事情,它们看起来都太强烈了(Date对象等).
我正在构建一个提供JSON或XML格式数据的API的Go库.
此API要求我session_id每15分钟左右请求一次,并在通话中使用它.例如:
foo.com/api/[my-application-id]/getuserprofilejson/[username]/[session-id]
foo.com/api/[my-application-id]/getuserprofilexml/[username]/[session-id]
Run Code Online (Sandbox Code Playgroud)
在我的Go库中,我正在尝试在main()func 之外创建一个变量,并打算对每个API调用的值执行ping操作.如果该值为nil或空,请求新的会话ID,依此类推.
package apitest
import (
"fmt"
)
test := "This is a test."
func main() {
fmt.Println(test)
test = "Another value"
fmt.Println(test)
}
Run Code Online (Sandbox Code Playgroud)
什么是惯用的Go方式来声明一个全局可访问的变量,但不是necesarilly常量?
我的test变量需要:
我正在运行virtualenv尝试学习Django,但无论出于何种原因安装Django后,当我尝试访问默认的Django起始页时,我在浏览器中收到以下错误:
发生服务器错误.请联系管理员.
在我运行服务器的终端窗口中显示以下错误:
配置不当:模块"django.contrib.auth.middleware"未定义"SessionAuthenticationMiddleware"属性/类
如果有人知道为什么我在virtualenv中遇到这个错误,我会很感激.不过,我可以让服务器在非virtualenv设置中正确运行.
这是完整的堆栈跟踪:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
File "/Users/jruecke/Python/JSON/lib/python2.7/site-packages/Django-1.6.5-py2.7.egg/django/core/handlers/wsgi.py", line 187, in __call__
self.load_middleware()
File "/Users/jruecke/Python/JSON/lib/python2.7/site-packages/Django-1.6.5-py2.7.egg/django/core/handlers/base.py", line 45, in load_middleware
mw_class = import_by_path(middleware_path)
File "/Users/jruecke/Python/JSON/lib/python2.7/site-packages/Django-1.6.5-py2.7.egg/django/utils/module_loading.py", line 31, in import_by_path
error_prefix, module_path, class_name))
ImproperlyConfigured: Module "django.contrib.auth.middleware" does not define a "SessionAuthenticationMiddleware" attribute/class
[16/Sep/2014 22:44:30] "GET / HTTP/1.1" 500 59
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
File "/Users/jruecke/Python/JSON/lib/python2.7/site-packages/Django-1.6.5-py2.7.egg/django/core/handlers/wsgi.py", line …Run Code Online (Sandbox Code Playgroud) 有没有办法确定JavaScript函数是否是绑定函数?
例:
var obj = {
x:1
};
function printX() {
document.write(this.x);
}
function takesACallback(cb) {
// how can one determine if this is a bounded function
// not just a function?
if (typeof cb === 'function') {
cb();
}
}
takesACallback(printX.bind(obj)); // 1
takesACallback(printX); // undefinedRun Code Online (Sandbox Code Playgroud)
也许这是一个重点.我不是在问为什么第二个调用打印未定义.
我正在尝试为顺序遍历编写递归生成器.
class Tree {
*inOrderTraversal() {
function* helper(node) {
if (node.left !== null) {
// this line is executed, but helper is not being called
helper(node.left);
}
yield node.value;
if (node.right !== null) {
helper(node.right);
}
}
for (let i of helper(this.root)) {
yield i;
}
}
// other methods omitted
}
Run Code Online (Sandbox Code Playgroud)
我这样称呼发电机:
const tree = new Tree();
tree.add(2);
tree.add(1);
tree.add(3);
for (let i of tree.inOrderTraversal()) {
console.log(i); // only prints 2
}
Run Code Online (Sandbox Code Playgroud)
为什么发电机只能屈服2?为什么它至少没有产生1之前2? …
我最近遇到了JavaScript Blob对象,我用它来初始化一个web worker,其中代码包含在文档的脚本标记中.
基于MDN文档:
Blob对象表示不可变的原始数据的类文件对象.Blob表示不一定采用JavaScript本机格式的数据.
听起来它像是一个把一堆东西放进去的东西,所有东西都共享一个MIME类型.我认为这是错误的,这个意见是不完整的?
为什么对象需要/有用?
我喜欢 jasmine.objectContaining 提供的部分对象匹配:
mySpy({
foo: 'bar',
bar: 'baz'
});
expect(mySpy).toHaveBeenCalledWith(jasmine.objectContaining({ foo: 'bar' }));
Run Code Online (Sandbox Code Playgroud)
有茉莉花相当于字符串吗?大致如下:
mySpy('fooBar', 'barBaz');
expect(mySpy).toHaveBeenCalledWith(jasmine.stringContaining('foo'), jasmine.any(String));
Run Code Online (Sandbox Code Playgroud)
我想看看一个特定的论点,而不诉诸 mySpy.calls 的断言:
mySpy('fooBar', 'barBaz');
expect(mySpy.calls.argsFor(0)[0]).toContain('foo');
Run Code Online (Sandbox Code Playgroud) 我尝试了以下两种方法但没有成功.
第一个是级联需求文件.
# requirements.txt
-r requirements/req2.txt
-r requirements/req3.txt
Run Code Online (Sandbox Code Playgroud)
# requirements/req2.txt
Django==1.7.7
Run Code Online (Sandbox Code Playgroud)
# requirements/req3.txt
-i https://testpypi.python.org/pypi
foo-bar==0.4
Run Code Online (Sandbox Code Playgroud)
pip install -r requirements.txt导致pip没有找到Django.
第二次尝试是将两个要求都包含在一个文件中:
-i https://pypi.python.org/pypi/
Django==1.7.7
-i https://testpypi.python.org/pypi
foo-bar==0.4
Run Code Online (Sandbox Code Playgroud)
pip install -r requirements.txt导致同样的错误,pip没有找到Django.
如何使用pip从不同服务器/ index-urls安装软件包?
我遇到了go run main.go产生错误的问题:
# command-line-arguments
./main.go:9: undefined: test
Run Code Online (Sandbox Code Playgroud)
但是命令go build && ./goruntest编译并运行程序就好了.
输出是:
嗨来自test()
嗨,来自sameFileTest()
您好,来自pkgtest.Test()
您好,来自pkgtest.Test1()
我有这样的目录设置:
go/src/github.com/username/goruntest/
pkgtest/
pkgtest.go
pkgtest1.go
main.go
test2.go
Run Code Online (Sandbox Code Playgroud)
这是代码.
main.go
package main
import (
"fmt"
"github.com/username/goruntest/pkgtest"
)
func main() {
fmt.Println(test()) // main.go:9
fmt.Println(sameFileTest())
fmt.Println(pkgtest.Test())
fmt.Println(pkgtest.Test1())
}
func sameFileTest() string {
return "Hi from sameFileTest()"
}
Run Code Online (Sandbox Code Playgroud)
gotest1.go
package main
func test() string {
return "Hi from test()"
}
Run Code Online (Sandbox Code Playgroud)
pkgtest/pkgtest.go
package pkgtest
func Test() string {
return "Hi from pkgtest.Test()"
} …Run Code Online (Sandbox Code Playgroud) 我有一个功能,功能的基本思想是改变a指向的东西.第一个版本可行,但第二个版本没有.
有人可以帮我理解这里发生了什么吗?
// this works
void swap(int **a) {
int *temp = malloc(sizeof(int) * 3);
temp[0] = 0;
temp[1] = 1;
temp[2] = 2;
*a = temp;
}
// this does not
void swap(int **a) {
*a = malloc(sizeof(int) * 3);
*a[0] = 0;
*a[1] = 1; // seg fault occurs on this line
*a[2] = 2;
}
Run Code Online (Sandbox Code Playgroud)
我这样称呼函数
int main() {
int b[] = {0,1};
int *a = b;
swap(&a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此外,两个函数不同时属于同一文件.