Code Monkey home page Code Monkey logo

wxtimer's Introduction

wxTimer

介绍:

用于在微信小程序中进行倒计时的组件。

功能:

1、最基础的当然就是倒计时功能了。
2、可以设置倒计时结束后执行的事件。
3、可以设置倒计时执行过程中每隔多少秒,执行一次对应的事件。

在JS中调用

引入
`var timer = require('../../plugins/wxTimer.js');  `
最简单的调用方式:
var wxTimer = new timer({
    beginTime:"00:00:10"
})
wxTimer.start(this);
wxTimer.stop();
开启多个计时器
//开启第一个定时器
var wxTimer1 = new timer({
    beginTime:"00:00:10",
    name:'wxTimer1',
    complete:function(){
        console.log("完成了")
    }
})
wxTimer1.start(this);

//开启第二个定时器
var wxTimer2 = new timer({
    beginTime:"00:01:11",
    name:'wxTimer2',
    complete:function(){
        console.log("完成了")
    }
})
wxTimer2.start(this);

倒计时结束后执行事件
var wxTimer = new timer({
    beginTime:"00:00:10",
    complete:function(){
        console.log("完成了")
    }
})
wxTimer.start(this);
间隔执行事件
var wxTimer = new timer({
    beginTime:"00:00:10",
    complete:function(){
        console.log("完成了")
    },
    interval:2,
    intervalFn:function(){
        console.log("过去了2秒");
    }
})
校准时间
wxTimer.calibration();
结束计时
wxTimer.stop();

在wxml中引用

单个计时器:
<view>显示剩余时间:{{wxTimer}}</view>
<view>显示剩余秒数:{{wxTimerSecond}}</view>
多个计时器:
<view>显示计时器1的剩余时间:{{wxTimerList['wxTimer1'].wxTimer}}</view>
<view>显示计时器2的剩余时间:{{wxTimerList['wxTimer2'].wxTimer}}</view>
<view>显示计时器1的剩余秒数:{{wxTimerList['wxTimer1'].wxTimerSecond}}</view>
<view>显示计时器2的剩余秒数:{{wxTimerList['wxTimer2'].wxTimerSecond}}</view>

注意:

1、由于内部需要调用到小程序的setData方法,所以我们需要把this传过去。
2、此方法会在page中生成一个名为wxTimer,wxTimerSecond和wxTimerList的数据,请保证这些key没有被占用 3、请在data中添加一条属性wxTimerList:{},否则将会报错。

其他参数:

1、beginTime 需要倒计时的时间,比如:"01:11:12",默认值为"00:00:00",也可以省略秒数,如:"01:10"

2、complete 倒计时归零0时的回调函数,如果为beginTime = "00:00:00"则立即调用

3、interval 倒计时的过程中,规定每隔几秒执行一次intervalFn,如果为0则永远不会执行,默认为1

4、intervalFn 每隔interval秒执行一次的函数。

历史更新

2017.03.29 加入同个页面开启多个计时器的功能。
2017.03.22 应对在息屏和挂起状态下倒计时无法进行的问题,加入了校准功能,可以在onShow()函数中直接调用wxTimer.calibration()来校准时间

wxtimer's People

Contributors

baqihg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

wxtimer's Issues

初始出错

var wxTimer = new timer({ beginTime:"00:00:10", complete:function(){ console.log("完成了") } }) wxTimer.start(this);
default

<为什么呢
require是成功的

app.js配置问题

globalData:{
userInfo : null,
timer : require('/urtil/wxTimer.js')
}
}
timer后面的冒号要改成分号?

使用定时执行的时候报错

使用方式:
1、在onLoad函数中
`` onLoad: function (options) {
var that = this;
var wxTimer = new timer({
beginTime: "00:00:10",
name: 'wxTimer',
complete: function () {
console.log("完成了")
},
interval: 2,
intervalFn: function () {
console.log("过去了2秒");
}
})
wxTimer.start(that);
}


报错:

WAService.js:3 thirdScriptError
Cannot set property 'wxTimer' of undefined;at "pages/user/user" page lifeCycleMethod onLoad function
TypeError: Cannot set property 'wxTimer' of undefined
at begin (http://771211658.appservice.open.weixin.qq.com/utils/wxTimer.js:32:27)
at Object.start (http://771211658.appservice.open.weixin.qq.com/utils/wxTimer.js:54:3)

调试:

var wxTimerList = self.data.wxTimerList;

		//更新计时器数组
		wxTimerList[that.name] = {
			wxTimer:tmpTimeStr,
            wxTimerSecond:wxTimerSecond,
		}

wxTimerList 为undefined

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.